1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkComputeHistogram2DOutliers.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright 2009 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkComputeHistogram2DOutliers.h"
//------------------------------------------------------------------------------
#include "vtkCollection.h"
#include "vtkDataArray.h"
#include "vtkDoubleArray.h"
#include "vtkImageData.h"
#include "vtkImageMedian3D.h"
#include "vtkImageGradientMagnitude.h"
#include "vtkIdList.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
#include "vtkSortDataArray.h"
#include "vtkTable.h"
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkComputeHistogram2DOutliers);
//------------------------------------------------------------------------------
vtkComputeHistogram2DOutliers::vtkComputeHistogram2DOutliers()
{
this->SetNumberOfInputPorts(3);
this->SetNumberOfOutputPorts(2);
this->PreferredNumberOfOutliers = 10;
this->BuildTime.Modified();
}
//------------------------------------------------------------------------------
vtkComputeHistogram2DOutliers::~vtkComputeHistogram2DOutliers()
{
}
//------------------------------------------------------------------------------
int vtkComputeHistogram2DOutliers::RequestData(
vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// get the output
vtkInformation *outSelectionInfo = outputVector->GetInformationObject(OUTPUT_SELECTED_ROWS);
vtkSelection* outputSelection = vtkSelection::SafeDownCast(
outSelectionInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkInformation *outTableInfo = outputVector->GetInformationObject(OUTPUT_SELECTED_TABLE_DATA);
vtkTable* outputTable = vtkTable::SafeDownCast(
outTableInfo->Get(vtkDataObject::DATA_OBJECT()));
// get the input table data
vtkInformation *inDataInfo = inputVector[INPUT_TABLE_DATA]->GetInformationObject(0);
if (!inDataInfo)
{
vtkErrorMacro("no input data information.");
return 0;
}
vtkTable *inData = vtkTable::SafeDownCast(inDataInfo->Get(vtkDataObject::DATA_OBJECT()));
if (!inData)
{
vtkErrorMacro("no input data table.");
return 0;
}
// get the input histogram data
// try the repeatable vtkImageData port first
vtkSmartPointer<vtkCollection> histograms = vtkSmartPointer<vtkCollection>::New();
int numHistograms = inputVector[INPUT_HISTOGRAMS_IMAGE_DATA]->GetNumberOfInformationObjects();
if (numHistograms > 0)
{
// get the data objects for the input
for (int i=0; i<numHistograms; i++)
{
vtkImageData* im = vtkImageData::SafeDownCast(
inputVector[INPUT_HISTOGRAMS_IMAGE_DATA]->GetInformationObject(i)->Get(vtkDataObject::DATA_OBJECT()));
if (!im)
{
vtkErrorMacro("invalid input histogram.");
return 0;
}
histograms->AddItem(im);
}
}
// if there wasn't anything on that port, try the vtkMultiBlockDataSet port
else
{
vtkInformation *inHistogramInfo = inputVector[INPUT_HISTOGRAMS_MULTIBLOCK]->GetInformationObject(0);
if (inHistogramInfo)
{
vtkMultiBlockDataSet* ds = vtkMultiBlockDataSet::SafeDownCast(
inHistogramInfo->Get(vtkDataObject::DATA_OBJECT()));
if (ds)
{
for (int i=0; i<(int)ds->GetNumberOfBlocks(); i++)
{
vtkImageData* im = vtkImageData::SafeDownCast(ds->GetBlock(i));
if (im)
{
histograms->AddItem(im);
}
}
}
}
}
if (histograms->GetNumberOfItems() <= 0)
{
vtkErrorMacro("No input histograms.");
return 0;
}
// compute the thresholds that contain outliers
vtkSmartPointer<vtkCollection> outlierThresholds = vtkSmartPointer<vtkCollection>::New();
if (!this->ComputeOutlierThresholds(histograms,outlierThresholds))
{
vtkErrorMacro("Error during outlier bin computation.");
return 0;
}
// take the computed outlier thresholds and extract the input table rows that match
vtkSmartPointer<vtkIdTypeArray> outlierRowIds = vtkSmartPointer<vtkIdTypeArray>::New();
if (outlierThresholds->GetNumberOfItems() >= 0 &&
!this->FillOutlierIds(inData,outlierThresholds,outlierRowIds,outputTable))
{
vtkErrorMacro("Error during outlier row retrieval.");
return 0;
}
// print out the table, just for grins
//outputTable->Dump();
// generate the selection based on the outlier row ids
if (outputSelection->GetNumberOfNodes() == 0)
{
vtkSmartPointer<vtkSelectionNode> newNode = vtkSmartPointer<vtkSelectionNode>::New();
newNode->GetProperties()->Set(
// vtkSelectionNode::CONTENT_TYPE(), vtkSelectionNode::PEDIGREEIDS);
vtkSelectionNode::CONTENT_TYPE(), vtkSelectionNode::INDICES);
newNode->GetProperties()->Set(
vtkSelectionNode::FIELD_TYPE(), vtkSelectionNode::ROW);
outputSelection->AddNode(newNode);
}
vtkSelectionNode* node = outputSelection->GetNode(0);
node->SetSelectionList(outlierRowIds);
this->BuildTime.Modified();
return 1;
}
//------------------------------------------------------------------------------
int vtkComputeHistogram2DOutliers::FillInputPortInformation(int port,
vtkInformation* info)
{
if (port == vtkComputeHistogram2DOutliers::INPUT_TABLE_DATA)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
return 1;
}
else if (port == vtkComputeHistogram2DOutliers::INPUT_HISTOGRAMS_IMAGE_DATA)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageData");
info->Set(vtkAlgorithm::INPUT_IS_REPEATABLE(), 1);
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
return 1;
}
else if (port == vtkComputeHistogram2DOutliers::INPUT_HISTOGRAMS_MULTIBLOCK)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkMultiBlockDataSet");
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
return 1;
}
return 0;
}
//------------------------------------------------------------------------------
int vtkComputeHistogram2DOutliers::FillOutputPortInformation(int port,
vtkInformation* info)
{
if (port == vtkComputeHistogram2DOutliers::OUTPUT_SELECTED_ROWS)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkSelection");
return 1;
}
else if (port == vtkComputeHistogram2DOutliers::OUTPUT_SELECTED_TABLE_DATA)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkTable");
return 1;
}
return 0;
}
//------------------------------------------------------------------------------
void vtkComputeHistogram2DOutliers::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << "PreferredNumberOfOutliers: " << this->PreferredNumberOfOutliers << endl;
}
//------------------------------------------------------------------------------
// Tries to find the right number of outliers. Not the smartest thing
// in the world yet. It basically starts off with a low percentage threshold
// (i.e. outlier bins must have a count smaller than pct * maximum bin cuont),
// find outliers, and grows the percentage if there are too outliers. The
// growth process is geometric until it finds enough, then it backtracks and
// goes linear. Very slow.
int vtkComputeHistogram2DOutliers::ComputeOutlierThresholds(vtkCollection* histograms, vtkCollection* thresholds)
{
if (!histograms || !thresholds)
return 0;
int numHistograms = histograms->GetNumberOfItems();
// compute the maximum bin count
double maxVal = 0.0;
double r[2];
for (int i=0; i<numHistograms; i++)
{
vtkImageData* histogram = vtkImageData::SafeDownCast(histograms->GetItemAsObject(i));
histogram->GetPointData()->GetScalars()->GetRange(r,0);
if (r[1] > maxVal)
maxVal = r[1];
}
double pctThreshold = .01;
bool growingSlower = false;
double slowGrowthInc = 100.0;
thresholds->RemoveAllItems();
// grow the percentage threshold until we're at 100% of the maximum bin count or
// we have enough outliers.
int numOutliers = 0;
while (pctThreshold < 1.0)
{
int tmpNumOutliers = 0;
vtkSmartPointer<vtkCollection> tmpThresholdCollection = vtkSmartPointer<vtkCollection>::New();
// compute outlier ids in all of the histograms
for (int i=0; i<numHistograms; i++)
{
vtkSmartPointer<vtkDoubleArray> tmpThresholds = vtkSmartPointer<vtkDoubleArray>::New();
tmpThresholds->SetNumberOfComponents(4);
vtkImageData* histogram = vtkImageData::SafeDownCast(histograms->GetItemAsObject(i));
tmpNumOutliers += this->ComputeOutlierThresholds(histogram,tmpThresholds,pctThreshold*maxVal);
tmpThresholdCollection->AddItem(tmpThresholds);
}
// Did the number of outliers get closer to the preferred number? If so, keep them.
if (abs(tmpNumOutliers - this->PreferredNumberOfOutliers) <=
abs(numOutliers - this->PreferredNumberOfOutliers))
{
thresholds->RemoveAllItems();
for (int j=0; j<tmpThresholdCollection->GetNumberOfItems(); j++)
thresholds->AddItem(tmpThresholdCollection->GetItemAsObject(j));
numOutliers = tmpNumOutliers;
}
// got farther from the preferred number, and still in the first pass. initiate second, slower pass.
else if (!growingSlower)
{
growingSlower = true;
pctThreshold *= .5;
slowGrowthInc = pctThreshold / 10.0;
}
// got farther from the preferred number, in the second class. quit.
else
{
break;
}
pctThreshold += (growingSlower) ? slowGrowthInc : pctThreshold;
}
return 1;
}
//------------------------------------------------------------------------------
// This function actually detects outliers, given a percentage threshold.
// It does a 3x3 median filter operation to find out what pixels disappear,
// and if they disappear and are small enough, the pixel is accepted as an outlier.
int vtkComputeHistogram2DOutliers::ComputeOutlierThresholds(vtkImageData* histogram, vtkDoubleArray* thresholds, double threshold)
{
if (!histogram || !thresholds)
return 0;
vtkSmartPointer<vtkImageMedian3D> median = vtkSmartPointer<vtkImageMedian3D>::New();
median->SetInputData(histogram);
median->SetKernelSize(3,3,1);
median->Update();
vtkDataArray* histArray = histogram->GetPointData()->GetScalars();
vtkDataArray* filtArray = median->GetOutput()->GetPointData()->GetScalars();
int dims[3] = { 0,0,0 };
double sp[3] = { 0,0,0 };
double o[3] = {0,0,0};
histogram->GetDimensions(dims);
histogram->GetSpacing(sp);
histogram->GetOrigin(o);
int x,y,numOutliers=0;
double hval,fval;
for (int j=0; j<histArray->GetNumberOfTuples(); j++)
{
hval = histArray->GetTuple1(j);
fval = filtArray->GetTuple1(j);
if (hval < threshold && hval-fval > 0.0)
{
x = j % dims[0];
y = j / dims[0];
thresholds->InsertNextTuple4(o[0] + x*sp[0], o[0] + (x+1)*sp[0],
o[1] + y*sp[1], o[1] + (y+1)*sp[1]);
numOutliers += (int)hval;
}
}
return numOutliers;
}
//------------------------------------------------------------------------------
int vtkComputeHistogram2DOutliers::FillOutlierIds(vtkTable* data, vtkCollection* thresholds, vtkIdTypeArray *rowIds, vtkTable* outTable)
{
if (!data || !thresholds || !rowIds || !outTable)
{
return 0;
}
// nothing to threshold, that's fine, just quit
if (thresholds->GetNumberOfItems() == 0)
{
return 1;
}
// if there's something to threshold, there better be the correct
// number of threshold arrays
else if (data->GetNumberOfColumns()-1 != thresholds->GetNumberOfItems())
{
return 0;
}
int numColumns = data->GetNumberOfColumns();
// store the matching rows in a vtkIdList since this list
// can check for uniqueness, and I don't want duplicate rows.
vtkSmartPointer<vtkIdList> uniqueRowIds = vtkSmartPointer<vtkIdList>::New();
for (int i=0; i<numColumns-1; i++)
{
vtkDataArray* col1 = vtkArrayDownCast<vtkDataArray>(data->GetColumn(i));
vtkDataArray* col2 = vtkArrayDownCast<vtkDataArray>(data->GetColumn(i+1));
vtkDoubleArray* currThresholds = vtkDoubleArray::SafeDownCast(thresholds->GetItemAsObject(i));
for (int j=0; j<currThresholds->GetNumberOfTuples(); j++)
{
double *t = currThresholds->GetTuple(j);
for (int k=0; k<col1->GetNumberOfTuples(); k++)
{
double v1 = col1->GetComponent(k,0);
double v2 = col2->GetComponent(k,0);
if (v1 >= t[0] && v1 < t[1] &&
v2 >= t[2] && v2 < t[3])
{
uniqueRowIds->InsertUniqueId(k);
}
}
}
}
rowIds->Initialize();
for (int i=0; i<uniqueRowIds->GetNumberOfIds(); i++)
{
rowIds->InsertNextValue(uniqueRowIds->GetId(i));
}
// this probably isn't necessary
vtkSortDataArray::Sort(rowIds);
// initialize the output table
outTable->Initialize();
for (int i=0; i<numColumns; i++)
{
vtkDataArray* a = vtkDataArray::CreateDataArray(data->GetColumn(i)->GetDataType());
a->SetNumberOfComponents(data->GetColumn(i)->GetNumberOfComponents());
a->SetName(data->GetColumn(i)->GetName());
outTable->AddColumn(a);
a->Delete();
}
for (int i=0; i<rowIds->GetNumberOfTuples(); i++)
{
outTable->InsertNextRow(data->GetRow(rowIds->GetValue(i)));
}
return 1;
}
//------------------------------------------------------------------------------
vtkTable* vtkComputeHistogram2DOutliers::GetOutputTable()
{
if (this->BuildTime < this->GetMTime())
this->Update();
return vtkTable::SafeDownCast(this->GetOutputDataObject(OUTPUT_SELECTED_TABLE_DATA));
}
|