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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageHistogramStatistics.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.
=========================================================================*/
#include "vtkImageHistogramStatistics.h"
#include "vtkObjectFactory.h"
#include "vtkIdTypeArray.h"
#include <math.h>
vtkStandardNewMacro(vtkImageHistogramStatistics);
//----------------------------------------------------------------------------
// Constructor sets default values
vtkImageHistogramStatistics::vtkImageHistogramStatistics()
{
this->AutomaticBinning = true;
this->GenerateHistogramImage = false;
this->Minimum = 0;
this->Maximum = 0;
this->Median = 0;
this->Mean = 0;
this->StandardDeviation = 0;
this->AutoRange[0] = 0;
this->AutoRange[1] = 1;
this->AutoRangePercentiles[0] = 1;
this->AutoRangePercentiles[1] = 99;
this->AutoRangeExpansionFactors[0] = 0.1;
this->AutoRangeExpansionFactors[1] = 0.1;
}
//----------------------------------------------------------------------------
vtkImageHistogramStatistics::~vtkImageHistogramStatistics()
{
}
//----------------------------------------------------------------------------
void vtkImageHistogramStatistics::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Minimum: " << this->Minimum << "\n";
os << indent << "Maximum: " << this->Maximum << "\n";
os << indent << "Median: " << this->Median << "\n";
os << indent << "Mean: " << this->Mean << "\n";
os << indent << "StandardDeviation: " << this->StandardDeviation << "\n";
os << indent << "AutoRange: " << this->AutoRange[0] << " "
<< this->AutoRange[1] << "\n";
os << indent << "AutoRangePercentiles: "
<< this->AutoRangePercentiles[0] << " "
<< this->AutoRangePercentiles[1] << "\n";
os << indent << "AutoRangeExpansionFactors: "
<< this->AutoRangeExpansionFactors[0] << " "
<< this->AutoRangeExpansionFactors[1] << "\n";
}
//----------------------------------------------------------------------------
int vtkImageHistogramStatistics::RequestData(
vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
this->Superclass::RequestData(request, inputVector, outputVector);
double lowPercentile = this->AutoRangePercentiles[0]*0.01;
double highPercentile = this->AutoRangePercentiles[1]*0.01;
vtkIdType total = this->Total;
vtkIdType sum = 0;
vtkIdType lowSum = static_cast<vtkIdType>(total*lowPercentile);
vtkIdType highSum = static_cast<vtkIdType>(total*highPercentile);
vtkIdType midSum = total/2;
vtkIdType lowVal = 0;
vtkIdType highVal = 0;
vtkIdType midVal = 0;
vtkIdType minVal = -1;
vtkIdType maxVal = 0;
double mom1 = 0;
double mom2 = 0;
vtkIdType nx = this->Histogram->GetNumberOfTuples();
vtkIdType *histogram = this->Histogram->GetPointer(0);
for (int ix = 0; ix < nx; ++ix)
{
vtkIdType c = histogram[ix];
sum += c;
double dc = static_cast<double>(c);
mom1 += dc*ix;
mom2 += dc*ix*ix;
lowVal = (sum > lowSum ? lowVal : ix);
highVal = (sum > highSum ? highVal : ix);
midVal = (sum > midSum ? midVal : ix);
minVal = (sum > 0 ? minVal : ix);
maxVal = (c == 0 ? maxVal : ix);
}
if (minVal < maxVal)
{
minVal++;
}
double binSpacing = this->BinSpacing;
double binOrigin = this->BinOrigin;
// do the basic statistics
this->Minimum = minVal*binSpacing + binOrigin;
this->Maximum = maxVal*binSpacing + binOrigin;
this->Median = midVal*binSpacing + binOrigin;
this->Mean = 0.0;
this->StandardDeviation = 0.0;
if (total > 0)
{
this->Mean = mom1/total*binSpacing + binOrigin;
}
if (total > 1)
{
double term2 = mom1*mom1/total;
if ((mom2 - term2) > 1e-10*mom2)
{
// use the fast method to compute standard deviation
this->StandardDeviation = sqrt((mom2 - term2)/(total - 1))*binSpacing;
}
else
{
// use more accurate method to avoid cancellation error
double xmean = mom1/total;
for (int ix = 0; ix < nx; ++ix)
{
double ixd = xmean - ix;
mom2 += ixd*ixd*histogram[ix];
}
this->StandardDeviation = sqrt(mom2/(total - 1))*binSpacing;
}
}
// do the autorange: first expand range by 10% at each end
double lowEF = this->AutoRangeExpansionFactors[0];
double highEF = this->AutoRangeExpansionFactors[1];
int lowExpansion = static_cast<int>(lowEF*(highVal - lowVal));
int highExpansion = static_cast<int>(highEF*(highVal - lowVal));
lowVal -= lowExpansion;
highVal += highExpansion;
this->AutoRange[0] = lowVal*binSpacing + binOrigin;
this->AutoRange[1] = highVal*binSpacing + binOrigin;
// clamp the auto range to the full data range
if (this->AutoRange[0] < this->Minimum)
{
this->AutoRange[0] = this->Minimum;
}
if (this->AutoRange[1] > this->Maximum)
{
this->AutoRange[1] = this->Maximum;
}
return 1;
}
|