]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx
First commit
[FrontAlgorithms.git] / lib / fpa / Image / RegionGrowWithMultipleThresholds.hxx
1 #ifndef __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__
2 #define __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__
3
4 #include <fpa/Image/Functors/RegionGrowThresholdFunction.h>
5
6 // -------------------------------------------------------------------------
7 template< class I >
8 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
9 AddThreshold( const TPixel& v )
10 {
11   this->m_Histogram[ v ] = 0;
12
13   typedef
14     fpa::Image::Functors::RegionGrowThresholdFunction< I >
15     TFunction;
16   TFunction* function =
17     dynamic_cast< TFunction* >( this->GetMembershipFunction( ) );
18   if( function != NULL )
19   {
20     function->SetLowerThreshold( this->m_Histogram.begin( )->first );
21     function->SetUpperThreshold( this->m_Histogram.rbegin( )->first );
22
23   } // fi
24   this->Modified( );
25 }
26
27 // -------------------------------------------------------------------------
28 template< class I >
29 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
30 AddThresholds(
31   const TPixel& t0,
32   const TPixel& t1,
33   const unsigned int& samples
34   )
35 {
36   double off =
37     ( double( t1 ) - double( t0 ) ) / ( double( samples ) - double( 1 ) );
38   for( unsigned int s = 0; s < samples; s++ )
39     this->AddThreshold( TPixel( ( double( s ) * off ) + double( t0 ) ) );
40 }
41
42 // -------------------------------------------------------------------------
43 template< class I >
44 fpa::Image::RegionGrowWithMultipleThresholds< I >::
45 RegionGrowWithMultipleThresholds( )
46   : Superclass( ),
47     m_DerivativeThreshold( double( 3 ) )
48 {
49   typedef
50     fpa::Image::Functors::RegionGrowThresholdFunction< I >
51     TFunction;
52   typename TFunction::Pointer function = TFunction::New( );
53   this->SetMembershipFunction( function );
54 }
55
56 // -------------------------------------------------------------------------
57 template< class I >
58 fpa::Image::RegionGrowWithMultipleThresholds< I >::
59 ~RegionGrowWithMultipleThresholds( )
60 {
61 }
62
63 // -------------------------------------------------------------------------
64 template< class I >
65 bool fpa::Image::RegionGrowWithMultipleThresholds< I >::
66 _UpdateResult( _TNode& n )
67 {
68   bool ret = this->Superclass::_UpdateResult( n );
69
70   if( ret )
71   {
72     TPixel v = TPixel( this->_Cost( n.Vertex, n.Vertex ) );
73
74     typename THistogram::reverse_iterator hIt = this->m_Histogram.rbegin( );
75     while( hIt != this->m_Histogram.rend( ) )
76     {
77       if( v <= hIt->first )
78       {
79         hIt->second += 1;
80         hIt++;
81       }
82       else
83         hIt = this->m_Histogram.rend( );
84
85     } // elihw
86     this->GetOutput( )->SetPixel( n.Vertex, v );
87
88   } // fi
89   return( ret );
90 }
91
92 // -------------------------------------------------------------------------
93 template< class I >
94 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
95 _AfterLoop( )
96 {
97   typename THistogram::iterator prevIt = this->m_Histogram.begin( );
98   typename THistogram::iterator currIt = prevIt; currIt++;
99   typename THistogram::iterator nextIt = currIt; nextIt++;
100   double prev_d1 = double( 0 );
101   for( ; nextIt != this->m_Histogram.end( ); ++prevIt, ++currIt, ++nextIt )
102   {
103     double d1 = double( nextIt->second ) - double( prevIt->second );
104     d1 /= double( nextIt->first ) - double( prevIt->first );
105
106     std::cout
107       << currIt->first  << " "
108       << currIt->second << " "
109       << d1 << " "
110       << ( d1 - prev_d1 ) << std::endl;
111
112     prev_d1 = d1;
113
114   } // rof
115
116
117   /*
118     double prev = double( 0 );
119     while( hIt != this->m_Histogram.end( ) )
120     {
121     double curr = double( hIt->second );
122     double deri = curr - prev;
123
124     if( hIt != this->m_Histogram.begin( ) )
125     std::cout << hIt->first << " " << curr << " " << deri << std::endl;
126
127     prev = curr;
128     hIt++;
129
130     } // rof
131   */
132   this->Superclass::_AfterLoop( );
133 }
134
135 #endif // __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__
136
137 // eof - $RCSfile$