]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/MoriRegionGrow.hxx
5c4efc1e921eb7879d37bdf575d083e983122f71
[FrontAlgorithms.git] / lib / fpa / Image / MoriRegionGrow.hxx
1 #ifndef __fpa__Image__MoriRegionGrow__hxx__
2 #define __fpa__Image__MoriRegionGrow__hxx__
3
4 // -------------------------------------------------------------------------
5 template< class _TInputImage, class _TOutputImage >
6 fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >::
7 MoriRegionGrow( )
8   : Superclass( ),
9     m_Step( TPixel( 1 ) ),
10     m_Sensitivity( double( 1 ) )
11 {
12   this->m_Upper = std::numeric_limits< TPixel >::max( );
13   if( std::numeric_limits< TPixel >::is_integer )
14     this->m_Lower = std::numeric_limits< TPixel >::min( );
15   else
16     this->m_Lower = -this->m_Upper;
17   typename TBinThresholdFunction::Pointer functor =
18     TBinThresholdFunction::New( );
19   this->SetGrowFunction( functor );
20 }
21
22 // -------------------------------------------------------------------------
23 template< class _TInputImage, class _TOutputImage >
24 fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >::
25 ~MoriRegionGrow( )
26 {
27 }
28
29 // -------------------------------------------------------------------------
30 template< class _TInputImage, class _TOutputImage >
31 bool fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >::
32 _ContinueGenerateData( )
33 {
34   TBinThresholdFunction* functor =
35     dynamic_cast< TBinThresholdFunction* >( this->GetGrowFunction( ) );
36   TPixel u = functor->GetUpper( );
37
38   // Analyze flooding
39   bool stop = false;
40   if( this->m_N > double( 10 ) )
41   {
42     double Q = double( this->m_PrevCount );
43     Q += ( this->m_STD * this->m_Sensitivity );
44     if( double( this->m_ActualCount ) > Q )
45       stop = true;
46
47   } // fi
48   double v;
49   if( this->m_PrevCount > 0 )
50     v = double( this->m_ActualCount ) - double( this->m_PrevCount );
51   else
52     v = double( 0 );
53   this->m_PrevCount = this->m_ActualCount;
54   this->m_N += double( 1 );
55   this->m_S1 += v;
56   this->m_S2 += v * v;
57   if( this->m_N > double( 1 ) )
58     this->m_STD =
59       ( this->m_S2 - ( ( this->m_S1 * this->m_S1 ) / this->m_N ) ) /
60       ( this->m_N - double( 1 ) );
61   else
62     this->m_STD = double( 0 );
63   this->m_STD = std::sqrt( this->m_STD );
64
65   std::cout << u << " " << this->m_ActualCount << " " << this->m_STD << std::endl;
66
67   if( u < this->m_Upper )
68   {
69     u += this->m_Step;
70     if( u > this->m_Upper )
71       u = this->m_Upper;
72     functor->SetUpper( u );
73     this->m_Queue = this->m_NextQueue;
74     while( this->m_NextQueue.size( ) > 0 )
75       this->m_NextQueue.pop( );
76     return( !stop );
77   }
78   else
79     return( false );
80 }
81
82 // -------------------------------------------------------------------------
83 template< class _TInputImage, class _TOutputImage >
84 void fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >::
85 _BeforeGenerateData( )
86 {
87   this->Superclass::_BeforeGenerateData( );
88
89   while( this->m_NextQueue.size( ) > 0 )
90     this->m_NextQueue.pop( );
91   this->m_ActualCount = 0;
92   this->m_PrevCount = 0;
93   this->m_N   = double( 0 );
94   this->m_S1  = double( 0 );
95   this->m_S2  = double( 0 );
96   this->m_STD = double( 0 );
97   TBinThresholdFunction* functor =
98     dynamic_cast< TBinThresholdFunction* >( this->GetGrowFunction( ) );
99   functor->SetLower( this->m_Lower );
100   functor->SetUpper( this->m_Lower + this->m_Step );
101 }
102
103 // -------------------------------------------------------------------------
104 template< class _TInputImage, class _TOutputImage >
105 void fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >::
106 _AfterGenerateData( )
107 {
108   this->Superclass::_AfterGenerateData( );
109   while( this->m_NextQueue.size( ) > 0 )
110     this->m_NextQueue.pop( );
111 }
112
113 // -------------------------------------------------------------------------
114 template< class _TInputImage, class _TOutputImage >
115 void fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >::
116 _BeforeLoop( )
117 {
118   this->Superclass::_BeforeLoop( );
119 }
120
121 // -------------------------------------------------------------------------
122 template< class _TInputImage, class _TOutputImage >
123 void fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >::
124 _AfterLoop( )
125 {
126   this->Superclass::_AfterLoop( );
127 }
128
129 // -------------------------------------------------------------------------
130 template< class _TInputImage, class _TOutputImage >
131 bool fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >::
132 _UpdateValue( _TQueueNode& v, const _TQueueNode& p )
133 {
134   bool ret = this->Superclass::_UpdateValue( v, p );
135   if( !ret )
136   {
137     v.Result = this->m_InsideValue;
138     this->m_NextQueue.push( v );
139
140   } // fi
141   return( ret );
142 }
143
144 // -------------------------------------------------------------------------
145 template< class _TInputImage, class _TOutputImage >
146 void fpa::Image::MoriRegionGrow< _TInputImage, _TOutputImage >::
147 _UpdateResult( const _TQueueNode& n )
148 {
149   this->Superclass::_UpdateResult( n );
150   this->m_ActualCount += 1;
151 }
152
153 #endif // __fpa__Image__MoriRegionGrow__hxx__
154
155 // eof - $RCSfile$