]> Creatis software - FrontAlgorithms.git/blob - libs/fpa/Image/MoriFilterHelper.hxx
...
[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   TPixel u = functor->GetUpper( );
36
37   // Update flooding data
38   this->m_Curve.push_back( TCurveData( u, this->m_ActualCount ) );
39   std::cout << u << " " << this->m_ActualCount << std::endl;
40
41   // Update thresholds
42   if( u < this->m_Upper )
43   {
44     u += this->m_Step;
45     if( u > this->m_Upper )
46       u = this->m_Upper;
47     functor->SetUpper( u );
48     this->m_Queue = this->m_NextQueue;
49     while( this->m_NextQueue.size( ) > 0 )
50       this->m_NextQueue.pop( );
51     return( true );
52   }
53   else
54     return( false );
55 }
56
57 // -------------------------------------------------------------------------
58 template< class _TInputImage, class _TOutputImage >
59 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
60 _BeforeGenerateData( )
61 {
62   this->Superclass::_BeforeGenerateData( );
63
64   while( this->m_NextQueue.size( ) > 0 )
65     this->m_NextQueue.pop( );
66   this->m_OptimumThreshold = ( typename _TOutputImage::PixelType )( 0 );
67   this->m_ActualCount = 0;
68   this->m_Curve.clear( );
69   TBinThresholdFunction* functor =
70     dynamic_cast< TBinThresholdFunction* >( this->GetGrowFunction( ) );
71   functor->SetLower( this->m_Lower );
72   functor->SetUpper( this->m_Lower + this->m_Step );
73 }
74
75 // -------------------------------------------------------------------------
76 template< class _TInputImage, class _TOutputImage >
77 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
78 _AfterGenerateData( )
79 {
80   typedef typename _TOutputImage::PixelType _TOut;
81
82   this->Superclass::_AfterGenerateData( );
83   while( this->m_NextQueue.size( ) > 0 )
84     this->m_NextQueue.pop( );
85
86   // Find optimum threshold by dicotomy
87   unsigned long l = 0;
88   unsigned long r = this->m_Curve.size( ) - 1;
89   while( ( r - l ) > 1 )
90   {
91     unsigned long m = ( r + l ) >> 1;
92     double vm = double( this->m_Curve[ m ].second );
93     double dl = vm - double( this->m_Curve[ l ].second );
94     double dr = double( this->m_Curve[ r ].second ) - vm;
95     if( dl > dr )
96       r = m;
97     else
98       l = m;
99
100   } // elihw
101   this->m_OptimumThreshold = _TOut( ( r + l ) >> 1 );
102 }
103
104 // -------------------------------------------------------------------------
105 template< class _TInputImage, class _TOutputImage >
106 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
107 _BeforeLoop( )
108 {
109   this->Superclass::_BeforeLoop( );
110 }
111
112 // -------------------------------------------------------------------------
113 template< class _TInputImage, class _TOutputImage >
114 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
115 _AfterLoop( )
116 {
117   this->Superclass::_AfterLoop( );
118 }
119
120 // -------------------------------------------------------------------------
121 template< class _TInputImage, class _TOutputImage >
122 bool fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
123 _UpdateValue( _TQueueNode& v, const _TQueueNode& p )
124 {
125   typedef typename _TOutputImage::PixelType _TOut;
126
127   bool ret = this->Superclass::_UpdateValue( v, p );
128   v.Result = _TOut( this->m_Curve.size( ) + 1 );
129   if( !ret )
130     this->m_NextQueue.push( v );
131   return( ret );
132 }
133
134 // -------------------------------------------------------------------------
135 template< class _TInputImage, class _TOutputImage >
136 void fpa::Image::MoriFilterHelper< _TInputImage, _TOutputImage >::
137 _UpdateResult( const _TQueueNode& n )
138 {
139   this->Superclass::_UpdateResult( n );
140   this->m_ActualCount += 1;
141 }
142
143 #endif // __fpa__Image__MoriFilterHelper__hxx__
144
145 // eof - $RCSfile$