/*========================================================================= Author: $Author: arnaudgelas $ // Author of last commit Version: $Rev: 567 $ // Revision of last commit Date: $Date: 2009-08-17 11:47:32 -0400 (Mon, 17 Aug 2009) $ // Date of last commit =========================================================================*/ /*========================================================================= Authors: The GoFigure Dev. Team. at Megason Lab, Systems biology, Harvard Medical school, 2009 Copyright (c) 2009, President and Fellows of Harvard College. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the President and Fellows of Harvard College nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =========================================================================*/ #ifndef __itkAdaptiveOtsuThresholdImageFilter_txx #define __itkAdaptiveOtsuThresholdImageFilter_txx #include "itkAdaptiveOtsuThresholdImageFilter.h" #include "itkExceptionObject.h" #include "itkProgressReporter.h" #include "itkNumericTraits.h" #include namespace itk { // Software Guide : BeginCodeSnippet template AdaptiveOtsuThresholdImageFilter:: AdaptiveOtsuThresholdImageFilter() { m_Radius.Fill( 8 ); m_NumberOfHistogramBins = 256; m_NumberOfControlPoints = 50; m_SplineOrder = 3; m_NumberOfLevels = 3; m_NumberOfSamples = 5000; m_OutsideValue = 0; m_InsideValue = 1; m_LowerThreshold = NumericTraits::NonpositiveMin(); m_UpperThreshold = NumericTraits::max(); this->Superclass::SetNumberOfRequiredInputs( 1 ); this->Superclass::SetNumberOfRequiredOutputs( 1 ); this->Superclass::SetNthOutput( 0, OutputImageType::New() ); } template typename AdaptiveOtsuThresholdImageFilter::PointSetPointer AdaptiveOtsuThresholdImageFilter ::ComputeRandomPointSet(InputConstImagePointer input) { InputImageRegionType inputRegion = input->GetRequestedRegion(); InputSizeType inputSize = inputRegion.GetSize(); InputIndexType inputIndex = inputRegion.GetIndex(); InputIndexType startIndex, endIndex; PointSetPointType point; // Find a random number generator RandomIteratorType rIt( input, inputRegion ); rIt.SetNumberOfSamples( m_NumberOfSamples ); rIt.GoToBegin(); PointSetPointer pointSet = PointSetType::New(); PointsContainerPointer pointscontainer = pointSet->GetPoints(); pointscontainer->Reserve( m_NumberOfSamples ); PointDataContainerPointer pointdatacontainer = PointDataContainer::New(); pointdatacontainer->Reserve( m_NumberOfSamples ); pointSet->SetPointData( pointdatacontainer ); unsigned long i = 0; while( !rIt.IsAtEnd() ) { startIndex = rIt.GetIndex(); /// Modification: take into account a non zero beginning index /// (happens when image is streamed). /// /// Leblanc Christophe. /// cleblancad@gmail.com /// 19/08/2013. for( unsigned int j = 0; j < ImageDimension; j++ ) { endIndex[j] = startIndex[j] + m_Radius[j] - 1; if( endIndex[j] >= static_cast< InputIndexValueType >( inputSize[j] ) + inputIndex[j] ) { startIndex[j] = inputSize[j] + inputIndex[j] - m_Radius[j]; } } input->TransformIndexToPhysicalPoint( startIndex, point ); pointscontainer->SetElement( i, point ); i++; ++rIt; } return pointSet; } template void AdaptiveOtsuThresholdImageFilter ::GenerateData() { // Allocate output this->AllocateOutputs(); this->GetOutput()->FillBuffer( 0 ); OutputImagePointer output = this->GetOutput(); InputConstImagePointer input = this->GetInput(); InputImageRegionType inputRegion = input->GetRequestedRegion(); OutputImageRegionType outputRegion = output->GetRequestedRegion(); /// Modification: throw an error if the radius /// is bigger than the image dimension. /// /// Leblanc Christophe. /// cleblancad@gmail.com /// 08/08/2013. for(unsigned int i = 0; i < TInputImage::ImageDimension; i++) { if(m_Radius[i] > inputRegion.GetSize()[i]) { std::stringstream ss; ss << "radius[" << i << "] > image dimension[" << i << "]"; throw ExceptionObject(__FILE__, __LINE__, ss.str()); } } /// Global threshold. /// Pixel elimination that are < m_LowerThreshold and > m_UpperThreshold. /// The other pixels remain unchanged. /*SPTIFilterPointer sptiFilter = SPTIFilterType::New(); sptiFilter->SetInput(input); sptiFilter->SetLowerThreshold(m_LowerThreshold); sptiFilter->SetUpperThreshold(m_UpperThreshold); sptiFilter->SetOutsideValue(m_OutsideValue); input = sptiFilter->GetOutput(); sptiFilter->Update(); inputRegion = input->GetRequestedRegion();*/ { /// Define a temporary image. typedef Image TempImageType; typename TempImageType::Pointer tempImagePtr; tempImagePtr = TempImageType::New(); tempImagePtr->SetRegions(inputRegion); tempImagePtr->Allocate(); InputPixelType p; ImageRegionIterator oIt(tempImagePtr, inputRegion); oIt.GoToBegin(); InputIteratorType iIt(input, inputRegion); iIt.GoToBegin(); while( !oIt.IsAtEnd() ) { p = iIt.Get(); if(p < m_LowerThreshold) oIt.Set(m_OutsideValue); else if(p > m_UpperThreshold) oIt.Set(m_OutsideValue); else oIt.Set(p); ++oIt; ++iIt; } input = tempImagePtr; } InputIndexType startIndex; InputImageRegionType region; region.SetSize( m_Radius ); PointSetPointType point; VectorPixelType V; PointSetPointer pointSet; pointSet = ComputeRandomPointSet(input); PointsContainerPointer pointscontainer = pointSet->GetPoints(); PointDataContainerPointer pointdatacontainer = pointSet->GetPointData(); for( unsigned long i = 0; i < m_NumberOfSamples; i++ ) { point = pointscontainer->GetElement( i ); input->TransformPhysicalPointToIndex( point, startIndex ); region.SetIndex( startIndex ); ROIFilterPointer roi = ROIFilterType::New(); roi->SetInput( input ); roi->SetRegionOfInterest( region ); roi->ReleaseDataFlagOn(); roi->Update(); OtsuThresholdPointer otsu = OtsuThresholdType::New(); otsu->SetImage( roi->GetOutput() ); otsu->SetNumberOfHistogramBins(m_NumberOfHistogramBins); otsu->Compute(); V[0] = static_cast( otsu->GetThreshold() ); pointdatacontainer->SetElement( i, V ); } typename SDAFilterType::ArrayType ncps; ncps.Fill( m_NumberOfControlPoints ); SDAFilterPointer filter = SDAFilterType::New(); filter->SetSplineOrder( m_SplineOrder ); filter->SetNumberOfControlPoints( ncps ); filter->SetNumberOfLevels( m_NumberOfLevels ); /// Modification: take into account a non zero beginning index /// (happens when image is streamed). /// /// Leblanc Christophe. /// cleblancad@gmail.com /// 19/08/2013. // Define the parametric domain. typename InputImageType::PointType origin; for(unsigned int i = 0; i < InputImageType::ImageDimension; i++) origin[i] = inputRegion.GetIndex()[i]; filter->SetOrigin( origin ); filter->SetSpacing( input->GetSpacing() ); filter->SetSize( inputRegion.GetSize() ); filter->SetInput( pointSet ); filter->Update(); filter->ReleaseDataFlagOn(); IndexFilterPointer componentExtractor = IndexFilterType::New(); componentExtractor->SetInput( filter->GetOutput() ); componentExtractor->SetIndex( 0 ); componentExtractor->Update(); componentExtractor->ReleaseDataFlagOn(); m_Threshold = componentExtractor->GetOutput(); /// Modification: add a progress. /// /// Leblanc Christophe. /// cleblancad@gmail.com /// 08/08/2013. ProgressReporter progress(this, 0, outputRegion.GetNumberOfPixels()); OutputIteratorType Itt( m_Threshold, m_Threshold->GetRequestedRegion() ); Itt.GoToBegin(); OutputIteratorType oIt( output, outputRegion ); oIt.GoToBegin(); InputIteratorType iIt( input, inputRegion ); iIt.GoToBegin(); OutputPixelType p; while( !oIt.IsAtEnd() ) { p = Itt.Get(); if ( p < iIt.Get() ) { oIt.Set( m_InsideValue ); } else { oIt.Set( m_OutsideValue ); } ++Itt; ++oIt; ++iIt; progress.CompletedPixel(); } } template void AdaptiveOtsuThresholdImageFilter:: PrintSelf( std::ostream& os, Indent indent ) const { Superclass::PrintSelf(os,indent); os << indent << "Radius size: " << GetRadius() << std::endl; os << indent << "Spline order: " << GetSplineOrder() << std::endl; os << indent << "Number Of control points: " << GetNumberOfControlPoints() << std::endl; os << indent << "Number of samples: " << GetNumberOfSamples() << std::endl; os << indent << "Number Of levels: " << GetNumberOfLevels() << std::endl; os << indent << "Number of histogram bins: " << GetNumberOfHistogramBins() << std::endl; os << indent << "Inside value: " << GetInsideValue() << std::endl; os << indent << "Outside value: " << GetOutsideValue() << std::endl; } /// ------------------------- SmallPartialThresholdImageFilter ------------------------- /*template SmallPartialThresholdImageFilter::SmallPartialThresholdImageFilter() { TOMOPROCESS_DEBUG_TEXT("template SmallPartialThresholdImageFilter::SmallPartialThresholdImageFilter()") m_OutsideValue = 0; m_LowerThreshold = NumericTraits::NonpositiveMin(); m_UpperThreshold = NumericTraits::max(); } template void SmallPartialThresholdImageFilter::GenerateData() { TOMOPROCESS_DEBUG_TEXT("template void SmallPartialThresholdImageFilter::GenerateData()") if(m_LowerThreshold > m_UpperThreshold) { itkExceptionMacro(<< "Lower threshold cannot be greater than upper threshold."); } // Allocate output. TOMOPROCESS_DEBUG_TEXT("Allocate output") this->AllocateOutputs(); OutputImagePointer output = this->GetOutput(); InputConstImagePointer input = this->GetInput(); InputImageRegionType inputRegion = input->GetRequestedRegion(); OutputImageRegionType outputRegion = output->GetRequestedRegion(); TOMOPROCESS_DEBUG_TEXT("Set values to image") ProgressReporter progress(this, 0, outputRegion.GetNumberOfPixels()); OutputPixelType p; OutputIteratorType oIt(output, outputRegion); oIt.GoToBegin(); InputIteratorType iIt(input, inputRegion); iIt.GoToBegin(); while( !oIt.IsAtEnd() ) { p = static_cast(iIt.Get()); if(p < m_LowerThreshold) oIt.Set(m_OutsideValue); else if(p > m_UpperThreshold) oIt.Set(m_OutsideValue); else oIt.Set(p); ++oIt; ++iIt; progress.CompletedPixel(); } } template void SmallPartialThresholdImageFilter:: PrintSelf(std::ostream &os, Indent indent) const { TOMOPROCESS_DEBUG_TEXT("template void SmallPartialThresholdImageFilter::PrintSelf(std::ostream &os, Indent indent) const") Superclass::PrintSelf(os, indent); os << indent << "Lower threshold: " << m_LowerThreshold << std::endl; os << indent << "Upper threshold: " << m_UpperThreshold << std::endl; }*/ } /* end namespace itk */ #endif