]> Creatis software - FrontAlgorithms.git/blob - libs/fpa/Image/MoriRegionGrowHelper.hxx
9dc1df3f713891f0b8289f42d3a17a239f1fa439
[FrontAlgorithms.git] / libs / fpa / Image / MoriRegionGrowHelper.hxx
1 #ifndef __fpa__Image__MoriRegionGrowHelper__hxx__
2 #define __fpa__Image__MoriRegionGrowHelper__hxx__
3
4 // -------------------------------------------------------------------------
5 template< class _TInputImage, class _TOutputImage >
6 fpa::Image::MoriRegionGrowHelper< _TInputImage, _TOutputImage >::
7 MoriRegionGrowHelper( )
8   : Superclass( ),
9     m_Step( TPixel( 1 ) )
10 {
11   this->m_Upper = std::numeric_limits< TPixel >::max( );
12   if( std::numeric_limits< TPixel >::is_integer )
13     this->m_Lower = std::numeric_limits< TPixel >::min( );
14   else
15     this->m_Lower = -this->m_Upper;
16   typename TBinThresholdFunction::Pointer functor =
17     TBinThresholdFunction::New( );
18   this->SetGrowFunction( functor );
19 }
20
21 // -------------------------------------------------------------------------
22 template< class _TInputImage, class _TOutputImage >
23 fpa::Image::MoriRegionGrowHelper< _TInputImage, _TOutputImage >::
24 ~MoriRegionGrowHelper( )
25 {
26 }
27
28 // -------------------------------------------------------------------------
29 template< class _TInputImage, class _TOutputImage >
30 bool fpa::Image::MoriRegionGrowHelper< _TInputImage, _TOutputImage >::
31 _ContinueGenerateData( )
32 {
33   TBinThresholdFunction* functor =
34     dynamic_cast< TBinThresholdFunction* >( this->GetGrowFunction( ) );
35   TPixel u = functor->GetUpper( );
36
37   // Update flooding data
38   this->m_Curve.push_back( TCurveData( u, this->m_ActualCount ) );
39
40   // Update thresholds
41   if( u < this->m_Upper )
42   {
43     u += this->m_Step;
44     if( u > this->m_Upper )
45       u = this->m_Upper;
46     functor->SetUpper( u );
47     this->m_Queue = this->m_NextQueue;
48     while( this->m_NextQueue.size( ) > 0 )
49       this->m_NextQueue.pop( );
50     return( true );
51   }
52   else
53     return( false );
54 }
55
56 // -------------------------------------------------------------------------
57 template< class _TInputImage, class _TOutputImage >
58 void fpa::Image::MoriRegionGrowHelper< _TInputImage, _TOutputImage >::
59 _BeforeGenerateData( )
60 {
61   this->Superclass::_BeforeGenerateData( );
62
63   while( this->m_NextQueue.size( ) > 0 )
64     this->m_NextQueue.pop( );
65   this->m_OptimumThreshold = ( typename _TOutputImage::PixelType )( 0 );
66   this->m_ActualCount = 0;
67   this->m_Curve.clear( );
68   TBinThresholdFunction* functor =
69     dynamic_cast< TBinThresholdFunction* >( this->GetGrowFunction( ) );
70   functor->SetLower( this->m_Lower );
71   functor->SetUpper( this->m_Lower + this->m_Step );
72 }
73
74 // -------------------------------------------------------------------------
75 template< class _TInputImage, class _TOutputImage >
76 void fpa::Image::MoriRegionGrowHelper< _TInputImage, _TOutputImage >::
77 _AfterGenerateData( )
78 {
79   typedef typename _TOutputImage::PixelType _TOut;
80
81   this->Superclass::_AfterGenerateData( );
82   while( this->m_NextQueue.size( ) > 0 )
83     this->m_NextQueue.pop( );
84
85   // Find optimum threshold by dicotomy
86   unsigned long l = 0;
87   unsigned long r = this->m_Curve.size( ) - 1;
88   while( ( r - l ) > 1 )
89   {
90     unsigned long m = ( r + l ) >> 1;
91     double vm = double( this->m_Curve[ m ].second );
92     double dl = vm - double( this->m_Curve[ l ].second );
93     double dr = double( this->m_Curve[ r ].second ) - vm;
94     if( dl > dr )
95       r = m;
96     else
97       l = m;
98
99   } // elihw
100   this->m_OptimumThreshold = _TOut( ( r + l ) >> 1 );
101 }
102
103 // -------------------------------------------------------------------------
104 template< class _TInputImage, class _TOutputImage >
105 void fpa::Image::MoriRegionGrowHelper< _TInputImage, _TOutputImage >::
106 _BeforeLoop( )
107 {
108   this->Superclass::_BeforeLoop( );
109 }
110
111 // -------------------------------------------------------------------------
112 template< class _TInputImage, class _TOutputImage >
113 void fpa::Image::MoriRegionGrowHelper< _TInputImage, _TOutputImage >::
114 _AfterLoop( )
115 {
116   this->Superclass::_AfterLoop( );
117 }
118
119 // -------------------------------------------------------------------------
120 template< class _TInputImage, class _TOutputImage >
121 bool fpa::Image::MoriRegionGrowHelper< _TInputImage, _TOutputImage >::
122 _UpdateValue( _TQueueNode& v, const _TQueueNode& p )
123 {
124   typedef typename _TOutputImage::PixelType _TOut;
125
126   bool ret = this->Superclass::_UpdateValue( v, p );
127   v.Result = _TOut( this->m_Curve.size( ) + 1 );
128   if( !ret )
129     this->m_NextQueue.push( v );
130   return( ret );
131 }
132
133 // -------------------------------------------------------------------------
134 template< class _TInputImage, class _TOutputImage >
135 void fpa::Image::MoriRegionGrowHelper< _TInputImage, _TOutputImage >::
136 _UpdateResult( const _TQueueNode& n )
137 {
138   this->Superclass::_UpdateResult( n );
139   this->m_ActualCount += 1;
140 }
141
142 #endif // __fpa__Image__MoriRegionGrowHelper__hxx__
143
144 // eof - $RCSfile$