]> 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 }
51
52 // -------------------------------------------------------------------------
53 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
54 RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
55 RandomWalkLabelling( )
56   : Superclass( ),
57     m_InsideLabel( TLabel( 1 ) ),
58     m_LowerLabel( TLabel( 3 ) ),
59     m_UpperLabel( TLabel( 4 ) ),
60     m_Radius( double( 1 ) ),
61     m_LowerThreshold( double( 0 ) ),
62     m_UpperThreshold( double( 0 ) ),
63     m_MaxCost( std::numeric_limits< TScalar >::max( ) )
64 {
65   fpaFilterInputConfigureMacro( InputCosts, TCostsImage );
66   fpaFilterInputConfigureMacro( InputPath, TPath );
67   this->SetOutsideLabel( TLabel( 2 ) );
68 }
69
70 // -------------------------------------------------------------------------
71 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
72 RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
73 ~RandomWalkLabelling( )
74 {
75 }
76
77 // -------------------------------------------------------------------------
78 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
79 void RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
80 _PrepareSeeds( const itk::DataObject* reference )
81 {
82   const TPath* path = this->GetInputPath( );
83   if( path->GetSize( ) > 0 )
84   {
85     TNode node;
86     node.Vertex = node.Parent = path->GetVertex( 0 );
87     node.FrontId = 1;
88     this->m_UnifiedSeeds.clear( );
89     this->m_UnifiedSeeds.insert( node );
90     this->m_CurrIdx = 0;
91
92   } // fi
93 }
94
95 // -------------------------------------------------------------------------
96 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
97 void RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
98 _PostComputeOutputValue( TNode& n )
99 {
100   const TRawImage* raw = this->GetInput( );
101   const TCostsImage* costs = this->GetInputCosts( );
102   const TPath* path = this->GetInputPath( );
103
104   TPoint c, p;
105   raw->TransformIndexToPhysicalPoint( path->GetVertex( this->m_CurrIdx ), c );
106   raw->TransformIndexToPhysicalPoint( n.Vertex, p );
107   double d = double( c.EuclideanDistanceTo( p ) );
108   if( d <= this->m_Radius )
109   {
110     n.FrontId = 1;
111     if( costs->GetPixel( n.Vertex ) == this->m_MaxCost )
112     {
113       double v = double( raw->GetPixel( n.Vertex ) );
114       if( v <= this->m_LowerThreshold )
115         n.Value = this->GetLowerLabel( );
116       else if( v >= this->m_UpperThreshold )
117         n.Value = this->GetUpperLabel( );
118       else
119         n.Value = TLabel( 0 );
120     }
121     else
122       n.Value = this->GetInsideLabel( );
123   }
124   else
125   {
126     n.Value = this->GetOutsideLabel( );
127     n.FrontId = 0;
128
129   } // fi
130 }
131
132 // -------------------------------------------------------------------------
133 template< class _TRawImage, class _TCostsImage, class _TLabelsImage >
134 void RandomWalkLabelling< _TRawImage, _TCostsImage, _TLabelsImage >::
135 _Reinitialize( )
136 {
137   const TPath* path = this->GetInputPath( );
138   while(
139     this->_GetMark( path->GetVertex( this->m_CurrIdx ) ) > 0 &&
140     this->m_CurrIdx < path->GetSize( )
141     )
142     this->m_CurrIdx += 1;
143   if( this->m_CurrIdx < path->GetSize( ) )
144   {
145     TNode node;
146     node.Vertex = node.Parent = path->GetVertex( this->m_CurrIdx );
147     node.FrontId = 1;
148     this->_QueuePush( node );
149
150   } // fi
151 }
152
153 #endif // __RandomWalkLabelling__hxx__
154
155 // eof - $RCSfile$