]> Creatis software - FrontAlgorithms.git/blob - appli/CTBronchi/MoriLabelling.hxx
...
[FrontAlgorithms.git] / appli / CTBronchi / MoriLabelling.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #ifndef __CTBronchi__MoriLabelling__hxx__
7 #define __CTBronchi__MoriLabelling__hxx__
8
9 #include <itkImageRegionConstIteratorWithIndex.h>
10
11 // -------------------------------------------------------------------------
12 template< class _TInputImage, class _TLabelImage >
13 const typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
14 TLabelImage* CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
15 GetInputLabelImage( ) const
16 {
17   return( this->GetLabels( ) );
18 }
19
20 // -------------------------------------------------------------------------
21 template< class _TInputImage, class _TLabelImage >
22 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
23 SetInputLabelImage( TLabelImage* image )
24 {
25   this->SetLabels( image );
26 }
27
28 // -------------------------------------------------------------------------
29 template< class _TInputImage, class _TLabelImage >
30 const typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
31 TInputImage* CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
32 GetInputRawImage( ) const
33 {
34   return( this->GetInput( ) );
35 }
36
37 // -------------------------------------------------------------------------
38 template< class _TInputImage, class _TLabelImage >
39 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
40 SetInputRawImage( TInputImage* image )
41 {
42   this->SetInput( image );
43 }
44
45 // -------------------------------------------------------------------------
46 template< class _TInputImage, class _TLabelImage >
47 typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
48 TInputValue CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
49 GetUpperThreshold( ) const
50 {
51   const TThresholdFunction* func =
52     dynamic_cast< const TThresholdFunction* >( this->GetValuePredicate( ) );
53   if( func != NULL )
54     return( func->GetUpper( ) );
55   else
56     return( TInputValue( 0 ) );
57 }
58
59 // -------------------------------------------------------------------------
60 template< class _TInputImage, class _TLabelImage >
61 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
62 SetUpperThreshold( TInputValue t )
63 {
64   TThresholdFunction* func =
65     dynamic_cast< TThresholdFunction* >( this->GetValuePredicate( ) );
66   if( func != NULL )
67     func->SetUpper( t );
68 }
69
70 // -------------------------------------------------------------------------
71 template< class _TInputImage, class _TLabelImage >
72 typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
73 TOutputValue CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
74 GetOutsideValue( ) const
75 {
76   return( this->GetInitValue( ) );
77 }
78
79 // -------------------------------------------------------------------------
80 template< class _TInputImage, class _TLabelImage >
81 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
82 SetOutsideLabel( TOutputValue o )
83 {
84   this->SetInitValue( o );
85 }
86
87 // -------------------------------------------------------------------------
88 template< class _TInputImage, class _TLabelImage >
89 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
90 MoriLabelling( )
91   : Superclass( ),
92     m_InsideLabel( TOutputValue( 0 ) )
93 {
94   typename TThresholdFunction::Pointer func = TThresholdFunction::New( );
95   this->SetPredicate( func );
96 }
97
98 // -------------------------------------------------------------------------
99 template< class _TInputImage, class _TLabelImage >
100 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
101 ~MoriLabelling( )
102 {
103 }
104
105 // -------------------------------------------------------------------------
106 template< class _TInputImage, class _TLabelImage >
107 typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
108 TNodes CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
109 _UnifySeeds( )
110 {
111   this->m_Seeds.clear( );
112   const TLabelImage* lbl = this->GetLabels( );
113   if( lbl == NULL )
114   {
115     std::ostringstream msg;
116     msg << "itk::ERROR: CTBronchi::MoriLabelling (" << this
117         << "): Labelled image not defined.";
118     ::itk::ExceptionObject e(
119       __FILE__, __LINE__, msg.str( ).c_str( ), ITK_LOCATION
120       );
121     throw e;
122
123   } // fi
124
125   // Iterate over labels
126   typename TLabelImage::RegionType reg = lbl->GetRequestedRegion( );
127   itk::ImageRegionConstIteratorWithIndex< TLabelImage > lIt( lbl, reg );
128   for( lIt.GoToBegin( ); !lIt.IsAtEnd( ); ++lIt )
129   {
130     if( lIt.Get( ) > 0 )
131     {
132       bool is_seed = false;
133       for( unsigned int d = 0; d < TLabelImage::ImageDimension; ++d )
134       {
135         for( int s = -1; s <= 1; s += 2 )
136         {
137           TVertex neigh = lIt.GetIndex( );
138           neigh[ d ] += s;
139           if( reg.IsInside( neigh ) )
140             is_seed |= ( lbl->GetPixel( neigh ) == 0 );
141
142         } // rof
143
144       } // rof
145
146       if( !is_seed )
147       {
148         typename TSeedsInterface::TNode node;
149         node.Vertex = lIt.GetIndex( );
150         node.Parent = lIt.GetIndex( );
151         node.FrontId = lIt.Get( );
152         node.Value = this->m_InsideLabel;
153         this->_Mark( node.Vertex, node.FrontId );
154         this->_UpdateOutputValue( node );
155       }
156       else
157       {
158         typename TSeedsInterface::TSeed seed;
159         seed.Vertex = lIt.GetIndex( );
160         seed.IsPoint = false;
161         seed.FrontId = lIt.Get( );
162         this->m_Seeds.push_back( seed );
163
164       } // fi
165
166     } // fi
167
168   } // rof
169
170   // Ok, finish initialization
171   return( this->Superclass::_UnifySeeds( ) );
172 }
173
174 // -------------------------------------------------------------------------
175 template< class _TInputImage, class _TLabelImage >
176 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
177 _UpdateOutputValue( TNode& n )
178 {
179   const TLabelImage* input_labels = this->GetInputLabelImage( );
180
181   this->Superclass::_UpdateOutputValue( n );
182   if( n.FrontId != 0 )
183   {
184     if( input_labels->GetPixel( n.Vertex ) == this->GetInsideValue( ) )
185       n.Value = this->GetInsideLabel( );
186     else
187       n.Value = TOutputValue( 0 );
188     this->TAlgorithm::_UpdateOutputValue( n );
189
190   } // fi
191 }
192
193 #endif // __CTBronchi__MoriLabelling__hxx__
194
195 // eof - $RCSfile$