]> Creatis software - FrontAlgorithms.git/blob - libs/fpa/Image/MoriFilterHelper.hxx
fb6826ad5e16de2ccaa08c1e5bf08236a72d8470
[FrontAlgorithms.git] / libs / fpa / Image / MoriFilterHelper.hxx
1 #ifndef __fpa__Image__MoriFilterHelper__hxx__
2 #define __fpa__Image__MoriFilterHelper__hxx__
3
4 // -------------------------------------------------------------------------
5 template< class _TInputImage, class _TOutputImage >
6 fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
7 MoriFilterHelper( )
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::MoriFilterHelper< _TInputImage, _TOutputImage >::
24 ~MoriFilterHelper( )
25 {
26 }
27
28 // -------------------------------------------------------------------------
29 template< class _TInputImage, class _TOutputImage >
30 bool fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
31 _ContinueGenerateData( )
32 {
33   TBinThresholdFunction* functor =
34     dynamic_cast< TBinThresholdFunction* >( this->GetGrowFunction( ) );
35
36   // Update flooding data
37   TPixel u = functor->GetUpper( );
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
48     while( this->m_Queue.size( ) > 0 )
49       this->m_Queue.pop( );
50     // TODO: std::cout << "-----> " << this->m_NextQueue.size( ) << " " << u << std::endl;
51     while( this->m_NextQueue.size( ) > 0 )
52     {
53       this->m_Queue.push( this->m_NextQueue.front( ) );
54       // TODO: std::cout << "\t" << this->m_NextQueue.front( ).Vertex << std::endl;
55       this->m_NextQueue.pop( );
56
57     } // elihw
58
59     /* TODO: ensure pixels belong to new threshold?
60        while( this->m_Queue.size( ) > 0 )
61        this->m_Queue.pop( );
62        while( this->m_NextQueue.size( ) > 0 )
63        {
64        _TQueueNode n = this->m_NextQueue.front( );
65        this->m_NextQueue.pop( );
66
67        if( ??? )
68        this->m_Queue.push( n );
69        }
70     */
71     return( true );
72   }
73   else
74     return( false );
75 }
76
77 // -------------------------------------------------------------------------
78 template< class _TInputImage, class _TOutputImage >
79 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
80 _BeforeGenerateData( )
81 {
82   this->Superclass::_BeforeGenerateData( );
83
84   while( this->m_NextQueue.size( ) > 0 )
85     this->m_NextQueue.pop( );
86   this->m_OptimumThreshold = ( typename _TOutputImage::PixelType )( 0 );
87   this->m_ActualCount = 0;
88   this->m_Curve.clear( );
89   TBinThresholdFunction* functor =
90     dynamic_cast< TBinThresholdFunction* >( this->GetGrowFunction( ) );
91   functor->SetLower( this->m_Lower );
92   functor->SetUpper( this->m_Lower + this->m_Step );
93 }
94
95 // -------------------------------------------------------------------------
96 template< class _TInputImage, class _TOutputImage >
97 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
98 _AfterGenerateData( )
99 {
100   typedef typename _TOutputImage::PixelType _TOut;
101
102   this->Superclass::_AfterGenerateData( );
103   while( this->m_NextQueue.size( ) > 0 )
104     this->m_NextQueue.pop( );
105
106   // Find optimum threshold by dicotomy
107   unsigned long l = 0;
108   unsigned long r = this->m_Curve.size( ) - 1;
109   while( ( r - l ) > 1 )
110   {
111     unsigned long m = ( r + l ) >> 1;
112     double vm = double( this->m_Curve[ m ].second );
113     double dl = vm - double( this->m_Curve[ l ].second );
114     double dr = double( this->m_Curve[ r ].second ) - vm;
115     if( dl > dr )
116       r = m;
117     else
118       l = m;
119
120   } // elihw
121   this->m_OptimumThreshold = _TOut( ( r + l ) >> 1 );
122 }
123
124 // -------------------------------------------------------------------------
125 template< class _TInputImage, class _TOutputImage >
126 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
127 _BeforeLoop( )
128 {
129   this->Superclass::_BeforeLoop( );
130 }
131
132 // -------------------------------------------------------------------------
133 template< class _TInputImage, class _TOutputImage >
134 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
135 _AfterLoop( )
136 {
137   this->Superclass::_AfterLoop( );
138 }
139
140 // -------------------------------------------------------------------------
141 template< class _TInputImage, class _TOutputImage >
142 bool fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
143 _UpdateValue( _TQueueNode& v, const _TQueueNode& p )
144 {
145   return( this->Superclass::_UpdateValue( v, p ) );
146   /* TODO
147      typedef typename _TOutputImage::PixelType _TOut;
148
149      bool ret = this->Superclass::_UpdateValue( v, p );
150      v.Result = _TOut( this->m_Curve.size( ) + 1 );
151      if( !ret )
152      this->m_NextQueue.push( v );
153      return( ret );
154   */
155 }
156
157 // -------------------------------------------------------------------------
158 template< class _TInputImage, class _TOutputImage >
159 bool fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
160 _UpdateResult( _TQueueNode& n )
161 {
162   typedef typename _TOutputImage::PixelType _TOut;
163
164   n.Result = _TOut( this->m_Curve.size( ) + 1 );
165   if( this->Superclass::_UpdateResult( n ) )
166   {
167     this->m_ActualCount += 1;
168     std::cout << this->m_ActualCount << std::endl;
169     return( true );
170   }
171   else
172   {
173     this->m_NextQueue.push( n );
174     return( false );
175
176   } // fi
177 }
178
179 #endif // __fpa__Image__MoriFilterHelper__hxx__
180
181 // eof - $RCSfile$