]> Creatis software - FrontAlgorithms.git/blob - appli/CTArteries/algorithms/RandomWalkLabelling.hxx
...
[FrontAlgorithms.git] / appli / CTArteries / algorithms / RandomWalkLabelling.hxx
1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4 #ifndef __RandomWalkerLabelling__hxx__
5 #define __RandomWalkerLabelling__hxx__
6
7 #include <limits>
8
9 // -------------------------------------------------------------------------
10 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
11 void RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
12 SetInputImage( const TRawImage* i )
13 {
14   this->SetInput( i );
15 }
16
17 // -------------------------------------------------------------------------
18 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
19 typename RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
20 TLabelsImage* RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
21 GetOutputLabels( )
22 {
23   return( this->GetOutput( ) );
24 }
25
26 // -------------------------------------------------------------------------
27 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
28 const typename RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
29 TLabelsImage* RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
30 GetOutputLabels( ) const
31 {
32   return( this->GetOutput( ) );
33 }
34
35 // -------------------------------------------------------------------------
36 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
37 typename RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
38 TLabel RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
39 GetOutsideLabel( ) const
40 {
41   return( this->GetInitValue( ) );
42 }
43
44 // -------------------------------------------------------------------------
45 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
46 void RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
47 SetOutsideLabel( const TLabel& v )
48 {
49   this->SetInitValue( v );
50   this->SetFillValue( v );
51 }
52
53 // -------------------------------------------------------------------------
54 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
55 RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
56 RandomWalkLabelling( )
57   : Superclass( ),
58     m_InsideLabel( TLabel( 1 ) ),
59     m_LowerLabel( TLabel( 3 ) ),
60     m_UpperLabel( TLabel( 4 ) ),
61     m_Radius( double( 1 ) ),
62     m_LowerThreshold( double( 0 ) ),
63     m_UpperThreshold( double( 0 ) ),
64     m_MaxCost( std::numeric_limits< TScalar >::max( ) )
65 {
66   fpaFilterInputConfigureMacro( InputCosts, TCostsImage );
67   fpaFilterInputConfigureMacro( InputPath, TPath );
68   this->SetOutsideLabel( TLabel( 2 ) );
69 }
70
71 // -------------------------------------------------------------------------
72 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
73 RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
74 ~RandomWalkLabelling( )
75 {
76 }
77
78 // -------------------------------------------------------------------------
79 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
80 void RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
81 _PrepareSeeds( const itk::DataObject* reference )
82 {
83   const TPath* path = this->GetInputPath( );
84   if( path->GetSize( ) > 0 )
85   {
86     TNode node;
87     node.Vertex = node.Parent = path->GetVertex( 0 );
88     node.FrontId = 1;
89     this->m_UnifiedSeeds.clear( );
90     this->m_UnifiedSeeds.insert( node );
91     this->m_CurrIdx = 0;
92
93   } // fi
94 }
95
96 // -------------------------------------------------------------------------
97 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
98 void RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
99 _PostComputeOutputValue( TNode& n )
100 {
101   const TRawImage* raw = this->GetInput( );
102   const TCostsImage* costs = this->GetInputCosts( );
103   const TPath* path = this->GetInputPath( );
104
105   TPoint c, p;
106   raw->TransformIndexToPhysicalPoint( path->GetVertex( this->m_CurrIdx ), c );
107   raw->TransformIndexToPhysicalPoint( n.Vertex, p );
108   double d = double( c.EuclideanDistanceTo( p ) );
109   if( d <= this->m_Radius )
110   {
111     n.FrontId = 1;
112     if( costs->GetPixel( n.Vertex ) == this->m_MaxCost )
113     {
114       double v = double( raw->GetPixel( n.Vertex ) );
115       if( v < this->m_LowerThreshold )
116         n.Value = this->GetLowerLabel( );
117       else if( this->m_UpperThreshold < v )
118         n.Value = this->GetUpperLabel( );
119       else
120         n.Value = TLabel( 0 );
121     }
122     else
123       n.Value = this->GetInsideLabel( );
124   }
125   else
126   {
127     n.Value = this->GetOutsideLabel( );
128     n.FrontId = 0;
129
130   } // fi
131 }
132
133 // -------------------------------------------------------------------------
134 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
135 void RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
136 _Reinitialize( )
137 {
138   const TPath* path = this->GetInputPath( );
139   if( this->m_CurrIdx < path->GetSize( ) )
140   {
141     while(
142       this->_GetMark( path->GetVertex( this->m_CurrIdx ) ) > 0 &&
143       this->m_CurrIdx < path->GetSize( )
144       )
145       this->m_CurrIdx += 1;
146     if( this->m_CurrIdx < path->GetSize( ) )
147     {
148       TNode node;
149       node.Vertex = node.Parent = path->GetVertex( this->m_CurrIdx );
150       node.FrontId = 1;
151       this->_QueuePush( node );
152
153     } // fi
154
155   } // fi
156 }
157
158 #endif // __RandomWalkLabelling__hxx__
159
160 // eof - $RCSfile$