ImageStatisticsCalculator::ImageStatisticsCalculator() : m_MaskingMode( MASKING_MODE_NONE ), m_MaskingModeChanged( false ), m_IgnorePixelValue(0.0), m_DoIgnorePixelValue(false), m_IgnorePixelValueChanged(false) { m_EmptyHistogram = HistogramType::New(); HistogramType::SizeType histogramSize; histogramSize.Fill( 256 ); m_EmptyHistogram->Initialize( histogramSize ); m_EmptyStatistics.Reset(); }
PartialVolumeAnalysisHistogramCalculator::PartialVolumeAnalysisHistogramCalculator() : m_MaskingMode( MASKING_MODE_NONE ), m_MaskingModeChanged( false ), m_NumberOfBins(256), m_UpsamplingFactor(1), m_GaussianSigma(0), m_ForceUpdate(false), m_PlanarFigureThickness(0) { m_EmptyHistogram = HistogramType::New(); HistogramType::SizeType histogramSize; histogramSize.Fill( 256 ); m_EmptyHistogram->Initialize( histogramSize ); m_EmptyStatistics.Reset(); }
USImageType::PixelType returnthresh( itk::SmartPointer<USImageType> input_image, int num_bin_levs, int num_in_fg ){ //Instantiate the different image and filter types that will be used typedef itk::ImageRegionConstIterator< USImageType > ConstIteratorType; typedef itk::Statistics::Histogram< float > HistogramType; typedef itk::OtsuMultipleThresholdsCalculator< HistogramType > CalculatorType; std::cout<<"Starting threshold computation\n"; //Create a temporary histogram container: const int numBins = itk::NumericTraits<USImageType::PixelType>::max(); double *tempHist; tempHist = (double*) malloc( sizeof(double) * numBins ); for(USImageType::PixelType i=0; i<numBins; ++i) tempHist[i] = 0; USImageType::PixelType maxval = itk::NumericTraits<USImageType::PixelType>::ZeroValue(); USImageType::PixelType minval = itk::NumericTraits<USImageType::PixelType>::max(); //Populate the histogram (assume pixel type is actually is some integer type): ConstIteratorType it( input_image, input_image->GetRequestedRegion() ); for ( it.GoToBegin(); !it.IsAtEnd(); ++it ){ USImageType::PixelType pix = it.Get(); ++tempHist[pix]; if( pix > maxval ) maxval = pix; if( pix < minval ) minval = pix; } //return max of type if there is no variation in the staining if( (maxval-minval)<3 ) return itk::NumericTraits<USImageType::PixelType>::max(); const USImageType::PixelType numBinsPresent = maxval+1; //Find max value in the histogram double floatIntegerMax = itk::NumericTraits<USImageType::PixelType>::max(); double max = 0.0; for(USImageType::PixelType i=0; i<numBinsPresent; ++i) if( tempHist[i] > max ) max = tempHist[i]; double scaleFactor = 1; if(max >= floatIntegerMax) scaleFactor = floatIntegerMax / max; HistogramType::Pointer histogram = HistogramType::New() ; // initialize histogram HistogramType::SizeType size; HistogramType::MeasurementVectorType lowerBound; HistogramType::MeasurementVectorType upperBound; lowerBound.SetSize(1); upperBound.SetSize(1); size.SetSize(1); lowerBound.Fill(0.0); upperBound.Fill((double)maxval); size.Fill(numBinsPresent); histogram->SetMeasurementVectorSize(1); histogram->Initialize(size, lowerBound, upperBound ) ; USImageType::PixelType i=0; for (HistogramType::Iterator iter = histogram->Begin(); iter != histogram->End(); ++iter ){ float norm_freq = (float)(tempHist[i] * scaleFactor); iter.SetFrequency(norm_freq); ++i; } std::cout<<"Histogram computed\n"; CalculatorType::Pointer calculator = CalculatorType::New(); calculator->SetNumberOfThresholds( num_bin_levs ); calculator->SetInputHistogram( histogram ); calculator->Update(); const CalculatorType::OutputType &thresholdVector = calculator->GetOutput(); CalculatorType::OutputType::const_iterator itNum = thresholdVector.begin(); float thresh; for(USImageType::PixelType i=0; i < num_in_fg; ++itNum, ++i) thresh = (static_cast<float>(*itNum)); std::cout<<"Threshold computed: "<<thresh<<std::endl; return (USImageType::PixelType)(thresh+0.5); }