1 #ifndef __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__
2 #define __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__
5 #include <itkBinaryThresholdImageFilter.h>
6 #include <itkConstNeighborhoodIterator.h>
8 // -------------------------------------------------------------------------
10 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
11 AddThreshold( const TPixel& v )
13 this->m_Thresholds.insert( v );
17 // -------------------------------------------------------------------------
19 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
20 AddThresholds( const TPixel& t0, const TPixel& t1, const unsigned int& s )
22 for( TPixel t = t0; t <= t1; t += TPixel( s ) )
23 this->AddThreshold( t );
26 // -------------------------------------------------------------------------
28 fpa::Image::RegionGrowWithMultipleThresholds< I >::
29 RegionGrowWithMultipleThresholds( )
31 m_InsideValue( TPixel( 1 ) ),
32 m_OutsideValue( TPixel( 0 ) ),
33 m_DifferenceThreshold( double( 3 ) ),
35 m_LastDiff( double( 0 ) ),
36 m_StopForced( false ),
37 m_StopThreshold( TPixel( 0 ) )
41 // -------------------------------------------------------------------------
43 fpa::Image::RegionGrowWithMultipleThresholds< I >::
44 ~RegionGrowWithMultipleThresholds( )
48 // -------------------------------------------------------------------------
50 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
53 const I* img = this->GetInput( );
56 this->ClearMembershipFunctions( );
57 this->m_Histogram.clear( );
58 this->m_TotalCount = img->GetLargestPossibleRegion( ).GetNumberOfPixels( );
59 this->m_LastDiff = double( -1 );
60 this->m_StopForced = false;
62 // Initialize thresholding functors
63 typename TThresholds::const_iterator tIt = this->m_Thresholds.begin( );
64 TPixel min_thr = std::numeric_limits< TPixel >::min( );
65 for( ; tIt != this->m_Thresholds.end( ); ++tIt )
67 typename TFunction::Pointer function = TFunction::New( );
68 function->SetInputImage( img );
69 function->SetLowerThreshold( min_thr );
70 function->SetUpperThreshold( *tIt );
71 this->AddMembershipFunction( function );
76 typename I::SizeType radius;
78 itk::ConstNeighborhoodIterator< I > it(
79 radius, img, img->GetRequestedRegion( )
81 for( unsigned int s = 0; s < this->m_Seeds.size( ); ++s )
83 _TNode seed = this->m_Seeds[ s ];
84 it.SetLocation( seed.Vertex );
86 typename I::SizeType size = it.GetSize( );
88 for( unsigned int d = 0; d < I::ImageDimension; ++d )
90 TPixel min_value = img->GetPixel( seed.Vertex );
91 for( unsigned int i = 0; i < nN; ++i )
93 if( it.GetPixel( i ) < min_value )
95 seed.Vertex = it.GetIndex( i );
96 min_value = it.GetPixel( i );
101 this->m_Seeds[ s ] = seed;
105 // Continue all initializations
106 this->Superclass::_BeforeMainLoop( );
109 // -------------------------------------------------------------------------
111 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
114 typedef itk::BinaryThresholdImageFilter< I, I > _TBinFilter;
116 // Binarize, inplace, the grown region
117 if( this->m_Histogram.size( ) > 1 )
119 typename _TBinFilter::Pointer bin = _TBinFilter::New( );
120 bin->SetInput( this->GetOutput( ) );
121 bin->SetInsideValue( this->m_InsideValue );
122 bin->SetOutsideValue( this->m_OutsideValue );
124 if( this->m_StopForced )
125 bin->SetUpperThreshold( this->m_StopThreshold );
127 bin->SetUpperThreshold( this->m_Histogram.rbegin( )->first );
128 bin->GraftOutput( this->GetOutput( ) );
130 this->GraftOutput( bin->GetOutput( ) );
134 this->Superclass::_AfterMainLoop( );
137 // -------------------------------------------------------------------------
139 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
142 if( this->m_ActualFunction != this->m_Functions.end( ) )
145 dynamic_cast< TFunction* >( this->m_ActualFunction->GetPointer( ) );
146 this->m_Histogram[ f->GetUpperThreshold( ) ] = this->m_Marks.size( );
148 /* TODO: code to print the signal for demo purposes
150 << f->GetUpperThreshold( ) << " "
151 << this->m_Marks.size( )
155 // Get the two last complete count, if any, and compute last
157 if( this->m_Histogram.size( ) > 1 )
159 typename THistogram::const_reverse_iterator hIt =
160 this->m_Histogram.rbegin( );
161 typename THistogram::const_reverse_iterator gIt = hIt;
164 this->m_LastDiff = double( hIt->second ) - double( gIt->second );
165 this->m_LastDiff /= double( hIt->first ) - double( gIt->first );
166 this->m_LastDiff *= m_DifferenceThreshold;
169 this->m_LastDiff = double( -1 );
172 this->Superclass::_AfterLoop( );
175 // -------------------------------------------------------------------------
177 bool fpa::Image::RegionGrowWithMultipleThresholds< I >::
178 _UpdateResult( _TNode& n )
180 // Here, the output is changed to the threshold used to compute the
181 // growing condition of the actual vertex n
182 bool ret = this->Superclass::_UpdateResult( n );
183 if( this->m_ActualFunction != this->m_Functions.end( ) )
186 dynamic_cast< TFunction* >( this->m_ActualFunction->GetPointer( ) );
188 this->GetOutput( )->SetPixel( n.Vertex, f->GetUpperThreshold( ) );
194 // -------------------------------------------------------------------------
196 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
197 _Mark( const _TNode& n )
199 this->Superclass::_Mark( n );
201 // Check if the histogram's support is enough
202 if( this->m_Histogram.size( ) < 2 )
204 typename THistogram::const_reverse_iterator hIt =
205 this->m_Histogram.rbegin( );
207 // Get the actual function
209 dynamic_cast< TFunction* >( this->m_ActualFunction->GetPointer( ) );
213 // Previous pixel count
214 unsigned long prev_aCount = hIt->second;
215 double prev_p = double( prev_aCount ) / double( this->m_TotalCount );
217 // Actual pixel count
218 unsigned long aCount = this->m_Marks.size( );
219 double p = double( aCount ) / double( this->m_TotalCount );
221 // Loop over, at least, 0.1% of the total number of pixels before
222 // performing stop analysis
223 if( double( 1e-3 ) < p && double( 1e-3 ) < prev_p )
225 // Does the difference is worthy to be analyzed?
226 if( aCount > hIt->second )
228 // Compute finite difference and compare it to the previous one
229 double diff = double( aCount ) - double( hIt->second );
230 diff /= double( f->GetUpperThreshold( ) ) - double( hIt->first );
231 if( diff > this->m_LastDiff )
233 this->m_StopForced = true;
234 this->m_StopThreshold = hIt->first;
243 // -------------------------------------------------------------------------
245 bool fpa::Image::RegionGrowWithMultipleThresholds< I >::
246 _CheckStopCondition( )
248 return( this->m_StopForced );
251 #endif // __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__