1 #ifndef __fpa__Image__MoriFilterHelper__hxx__
2 #define __fpa__Image__MoriFilterHelper__hxx__
4 // -------------------------------------------------------------------------
5 template< class _TInputImage, class _TOutputImage >
6 fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
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( );
15 this->m_Lower = -this->m_Upper;
16 typename TBinThresholdFunction::Pointer functor =
17 TBinThresholdFunction::New( );
18 this->SetGrowFunction( functor );
21 // -------------------------------------------------------------------------
22 template< class _TInputImage, class _TOutputImage >
23 fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
28 // -------------------------------------------------------------------------
29 template< class _TInputImage, class _TOutputImage >
30 bool fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
31 _ContinueGenerateData( )
33 TBinThresholdFunction* functor =
34 dynamic_cast< TBinThresholdFunction* >( this->GetGrowFunction( ) );
36 // Update flooding data
37 TPixel u = functor->GetUpper( );
38 this->m_Curve.push_back( TCurveData( u, this->m_ActualCount ) );
41 if( u < this->m_Upper )
44 if( u > this->m_Upper )
46 functor->SetUpper( u );
48 while( this->m_Queue.size( ) > 0 )
50 // TODO: std::cout << "-----> " << this->m_NextQueue.size( ) << " " << u << std::endl;
51 while( this->m_NextQueue.size( ) > 0 )
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( );
59 /* TODO: ensure pixels belong to new threshold?
60 while( this->m_Queue.size( ) > 0 )
62 while( this->m_NextQueue.size( ) > 0 )
64 _TQueueNode n = this->m_NextQueue.front( );
65 this->m_NextQueue.pop( );
68 this->m_Queue.push( n );
77 // -------------------------------------------------------------------------
78 template< class _TInputImage, class _TOutputImage >
79 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
80 _BeforeGenerateData( )
82 this->Superclass::_BeforeGenerateData( );
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 );
95 // -------------------------------------------------------------------------
96 template< class _TInputImage, class _TOutputImage >
97 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
100 typedef typename _TOutputImage::PixelType _TOut;
102 this->Superclass::_AfterGenerateData( );
103 while( this->m_NextQueue.size( ) > 0 )
104 this->m_NextQueue.pop( );
106 // Find optimum threshold by dicotomy
108 unsigned long r = this->m_Curve.size( ) - 1;
109 while( ( r - l ) > 1 )
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;
121 this->m_OptimumThreshold = _TOut( ( r + l ) >> 1 );
124 // -------------------------------------------------------------------------
125 template< class _TInputImage, class _TOutputImage >
126 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
129 this->Superclass::_BeforeLoop( );
132 // -------------------------------------------------------------------------
133 template< class _TInputImage, class _TOutputImage >
134 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
137 this->Superclass::_AfterLoop( );
140 // -------------------------------------------------------------------------
141 template< class _TInputImage, class _TOutputImage >
142 bool fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
143 _UpdateValue( _TQueueNode& v, const _TQueueNode& p )
145 return( this->Superclass::_UpdateValue( v, p ) );
147 typedef typename _TOutputImage::PixelType _TOut;
149 bool ret = this->Superclass::_UpdateValue( v, p );
150 v.Result = _TOut( this->m_Curve.size( ) + 1 );
152 this->m_NextQueue.push( v );
157 // -------------------------------------------------------------------------
158 template< class _TInputImage, class _TOutputImage >
159 bool fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
160 _UpdateResult( _TQueueNode& n )
162 typedef typename _TOutputImage::PixelType _TOut;
164 n.Result = _TOut( this->m_Curve.size( ) + 1 );
165 if( this->Superclass::_UpdateResult( n ) )
167 this->m_ActualCount += 1;
168 std::cout << this->m_ActualCount << std::endl;
173 this->m_NextQueue.push( n );
179 #endif // __fpa__Image__MoriFilterHelper__hxx__