]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx
Some visualizaton added
[FrontAlgorithms.git] / lib / fpa / Image / RegionGrowWithMultipleThresholds.hxx
1 #ifndef __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__
2 #define __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__
3
4 #include <limits>
5 #include <itkBinaryThresholdImageFilter.h>
6 #include <itkConstNeighborhoodIterator.h>
7
8 // -------------------------------------------------------------------------
9 template< class I >
10 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
11 AddThreshold( const TPixel& v )
12 {
13   this->m_Thresholds.insert( v );
14   this->Modified( );
15 }
16
17 // -------------------------------------------------------------------------
18 template< class I >
19 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
20 AddThresholds( const TPixel& t0, const TPixel& t1, const unsigned int& s )
21 {
22   for( TPixel t = t0; t <= t1; t += TPixel( s ) )
23     this->AddThreshold( t );
24 }
25
26 // -------------------------------------------------------------------------
27 template< class I >
28 fpa::Image::RegionGrowWithMultipleThresholds< I >::
29 RegionGrowWithMultipleThresholds( )
30   : Superclass( ),
31     m_InsideValue( TPixel( 1 ) ),
32     m_OutsideValue( TPixel( 0 ) ),
33     m_DifferenceThreshold( double( 3 ) ),
34     m_TotalCount( 0 ),
35     m_LastDiff( double( 0 ) ),
36     m_StopForced( false ),
37     m_StopThreshold( TPixel( 0 ) )
38 {
39 }
40
41 // -------------------------------------------------------------------------
42 template< class I >
43 fpa::Image::RegionGrowWithMultipleThresholds< I >::
44 ~RegionGrowWithMultipleThresholds( )
45 {
46 }
47
48 // -------------------------------------------------------------------------
49 template< class I >
50 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
51 _BeforeMainLoop( )
52 {
53   const I* img = this->GetInput( );
54
55   // Clear all states
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;
61
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 )
66   {
67     typename TFunction::Pointer function = TFunction::New( );
68     function->SetInputImage( img );
69     function->SetLowerThreshold( min_thr );
70     function->SetUpperThreshold( *tIt );
71     this->AddMembershipFunction( function );
72
73   } // rof
74
75   // Correct seeds
76   typename I::SizeType radius;
77   radius.Fill( 3 );
78   itk::ConstNeighborhoodIterator< I > it(
79     radius, img, img->GetRequestedRegion( )
80     );
81   for( unsigned int s = 0; s < this->m_Seeds.size( ); ++s )
82   {
83     _TNode seed = this->m_Seeds[ s ];
84     it.SetLocation( seed.Vertex );
85
86     typename I::SizeType size = it.GetSize( );
87     unsigned int nN = 1;
88     for( unsigned int d = 0; d < I::ImageDimension; ++d )
89       nN *= size[ d ];
90     TPixel min_value = img->GetPixel( seed.Vertex );
91     for( unsigned int i = 0; i < nN; ++i )
92     {
93       if( it.GetPixel( i ) < min_value )
94       {
95         seed.Vertex = it.GetIndex( i );
96         min_value = it.GetPixel( i );
97
98       } // fi
99
100     } // rof
101     this->m_Seeds[ s ] = seed;
102
103   } // rof
104
105   // Continue all initializations
106   this->Superclass::_BeforeMainLoop( );
107 }
108
109 // -------------------------------------------------------------------------
110 template< class I >
111 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
112 _AfterMainLoop( )
113 {
114   typedef itk::BinaryThresholdImageFilter< I, I > _TBinFilter;
115
116   // Binarize, inplace, the grown region
117   if( this->m_Histogram.size( ) > 1 )
118   {
119     typename _TBinFilter::Pointer bin = _TBinFilter::New( );
120     bin->SetInput( this->GetOutput( ) );
121     bin->SetInsideValue( this->m_InsideValue );
122     bin->SetOutsideValue( this->m_OutsideValue );
123     bin->InPlaceOn( );
124     if( this->m_StopForced )
125       bin->SetUpperThreshold( this->m_StopThreshold );
126     else
127       bin->SetUpperThreshold( this->m_Histogram.rbegin( )->first );
128     bin->GraftOutput( this->GetOutput( ) );
129     bin->Update( );
130     this->GraftOutput( bin->GetOutput( ) );
131
132   } // fi
133
134   this->Superclass::_AfterMainLoop( );
135 }
136
137 // -------------------------------------------------------------------------
138 template< class I >
139 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
140 _AfterLoop( )
141 {
142   if( this->m_ActualFunction != this->m_Functions.end( ) )
143   {
144     TFunction* f =
145       dynamic_cast< TFunction* >( this->m_ActualFunction->GetPointer( ) );
146     this->m_Histogram[ f->GetUpperThreshold( ) ] = this->m_Marks.size( );
147
148     /* TODO: code to print the signal for demo purposes
149        std::cout
150        << f->GetUpperThreshold( ) << " "
151        << this->m_Marks.size( )
152        << std::endl;
153     */
154
155     // Get the two last complete count, if any, and compute last
156     // finite difference
157     if( this->m_Histogram.size( ) > 1 )
158     {
159       typename THistogram::const_reverse_iterator hIt =
160         this->m_Histogram.rbegin( );
161       typename THistogram::const_reverse_iterator gIt = hIt;
162       gIt++;
163
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;
167     }
168     else
169       this->m_LastDiff = double( -1 );
170     
171   } // fi
172   this->Superclass::_AfterLoop( );
173 }
174
175 // -------------------------------------------------------------------------
176 template< class I >
177 bool fpa::Image::RegionGrowWithMultipleThresholds< I >::
178 _UpdateResult( _TNode& n )
179 {
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( ) )
184   {
185     TFunction* f =
186       dynamic_cast< TFunction* >( this->m_ActualFunction->GetPointer( ) );
187     if( f != NULL )
188       this->GetOutput( )->SetPixel( n.Vertex, f->GetUpperThreshold( ) );
189
190   } // fi
191   return( ret );
192 }
193
194 // -------------------------------------------------------------------------
195 template< class I >
196 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
197 _Mark( const _TNode& n )
198 {
199   this->Superclass::_Mark( n );
200
201   // Check if the histogram's support is enough
202   if( this->m_Histogram.size( ) < 2 )
203     return;
204   typename THistogram::const_reverse_iterator hIt =
205     this->m_Histogram.rbegin( );
206
207   // Get the actual function
208   TFunction* f =
209     dynamic_cast< TFunction* >( this->m_ActualFunction->GetPointer( ) );
210   if( f == NULL )
211     return;
212
213   // Previous pixel count
214   unsigned long prev_aCount = hIt->second;
215   double prev_p = double( prev_aCount ) / double( this->m_TotalCount );
216
217   // Actual pixel count
218   unsigned long aCount = this->m_Marks.size( );
219   double p = double( aCount ) / double( this->m_TotalCount );
220
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 )
224   {
225     // Does the difference is worthy to be analyzed?
226     if( aCount > hIt->second )
227     {
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 )
232       {
233         this->m_StopForced = true;
234         this->m_StopThreshold = hIt->first;
235
236       } // fi
237
238     } // fi
239     
240   } // fi
241 }
242
243 // -------------------------------------------------------------------------
244 template< class I >
245 bool fpa::Image::RegionGrowWithMultipleThresholds< I >::
246 _CheckStopCondition( )
247 {
248   return( this->m_StopForced );
249 }
250
251 #endif // __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__
252
253 // eof - $RCSfile$