]> 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 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
73 MoriLabelling( )
74   : Superclass( ),
75     m_InsideLabel( TOutputValue( 0 ) ),
76     m_OutsideLabel( TOutputValue( 0 ) )
77 {
78   typename TThresholdFunction::Pointer func = TThresholdFunction::New( );
79   this->SetPredicate( func );
80 }
81
82 // -------------------------------------------------------------------------
83 template< class _TInputImage, class _TLabelImage >
84 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
85 ~MoriLabelling( )
86 {
87 }
88
89 // -------------------------------------------------------------------------
90 template< class _TInputImage, class _TLabelImage >
91 typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
92 TNodes CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
93 _UnifySeeds( )
94 {
95   this->m_Seeds.clear( );
96   const TLabelImage* lbl = this->GetLabels( );
97   if( lbl == NULL )
98   {
99     std::ostringstream msg;
100     msg << "itk::ERROR: CTBronchi::MoriLabelling (" << this
101         << "): Labelled image not defined.";
102     ::itk::ExceptionObject e(
103       __FILE__, __LINE__, msg.str( ).c_str( ), ITK_LOCATION
104       );
105     throw e;
106
107   } // fi
108
109   // Iterate over labels
110   typename TLabelImage::RegionType reg = lbl->GetRequestedRegion( );
111   itk::ImageRegionConstIteratorWithIndex< TLabelImage > lIt( lbl, reg );
112   for( lIt.GoToBegin( ); !lIt.IsAtEnd( ); ++lIt )
113   {
114     if( lIt.Get( ) > 0 )
115     {
116       bool is_seed = false;
117       for( unsigned int d = 0; d < TLabelImage::ImageDimension; ++d )
118       {
119         for( int s = -1; s <= 1; s += 2 )
120         {
121           TVertex neigh = lIt.GetIndex( );
122           neigh[ d ] += s;
123           if( reg.IsInside( neigh ) )
124             is_seed |= ( lbl->GetPixel( neigh ) == 0 );
125
126         } // rof
127
128       } // rof
129
130       if( !is_seed )
131       {
132         typename TSeedsInterface::TNode node;
133         node.Vertex = lIt.GetIndex( );
134         node.Parent = lIt.GetIndex( );
135         node.FrontId = lIt.Get( );
136         node.Value = this->m_InsideLabel;
137         this->_Mark( node.Vertex, node.FrontId );
138         this->_UpdateOutputValue( node );
139       }
140       else
141       {
142         typename TSeedsInterface::TSeed seed;
143         seed.Vertex = lIt.GetIndex( );
144         seed.IsPoint = false;
145         seed.FrontId = lIt.Get( );
146         this->m_Seeds.push_back( seed );
147
148       } // fi
149
150     } // fi
151
152   } // rof
153
154   // Ok, finish initialization
155   return( this->Superclass::_UnifySeeds( ) );
156 }
157
158 // -------------------------------------------------------------------------
159 template< class _TInputImage, class _TLabelImage >
160 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
161 _UpdateOutputValue( TNode& n )
162 {
163   const TInputImage* input = this->GetInputRawImage( );
164   const TLabelImage* labels = this->GetInputLabelImage( );
165 }
166
167
168 /* TODO
169    TOutputValue m_InsideValue;
170    TOutputValue m_InsideLabel;
171    TOutputValue m_OutsideLabel;
172 */
173
174
175
176
177
178
179
180
181
182
183 /* TODO
184 #include <itkImageRegionConstIterator.h>
185 #include <itkImageRegionIterator.h>
186
187 // -------------------------------------------------------------------------
188 template< class _TInputImage, class _TLabelImage >
189 const _TLabelImage*
190 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
191 GetInputLabelImage( ) const
192 {
193   return( this->GetInput( ) );
194 }
195
196 // -------------------------------------------------------------------------
197 template< class _TInputImage, class _TLabelImage >
198 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
199 SetInputLabelImage( TLabelImage* image )
200 {
201   this->SetInput( image );
202 }
203
204 // -------------------------------------------------------------------------
205 template< class _TInputImage, class _TLabelImage >
206 const _TInputImage*
207 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
208 GetInputRawImage( ) const
209 {
210   return(
211     dynamic_cast< const TInputImage* >(
212       this->itk::ProcessObject::GetInput( 1 )
213       )
214     );
215 }
216
217 // -------------------------------------------------------------------------
218 template< class _TInputImage, class _TLabelImage >
219 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
220 SetInputRawImage( TInputImage* image )
221 {
222   this->itk::ProcessObject::SetNthInput( 1, image );
223 }
224
225 // -------------------------------------------------------------------------
226 template< class _TInputImage, class _TLabelImage >
227 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
228 MoriLabelling( )
229   : Superclass( ),
230     m_UpperThreshold( TOutputValue( 0 ) ),
231     m_InsideValue( TOutputValue( 0 ) ),
232     m_InsideLabel( TOutputValue( 0 ) ),
233     m_OutsideLabel( TOutputValue( 0 ) )
234 {
235   this->SetNumberOfRequiredInputs( 2 );
236 }
237
238 // -------------------------------------------------------------------------
239 template< class _TInputImage, class _TLabelImage >
240 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
241 ~MoriLabelling( )
242 {
243 }
244
245 // -------------------------------------------------------------------------
246 template< class _TInputImage, class _TLabelImage >
247 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
248 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType threadId )
249 {
250   itk::ImageRegionConstIterator< TLabelImage >
251     lIt( this->GetInputLabelImage( ), region );
252   itk::ImageRegionConstIterator< TInputImage >
253     iIt( this->GetInputRawImage( ), region );
254   itk::ImageRegionIterator< TLabelImage >
255     oIt( this->GetOutput( ), region );
256
257   lIt.GoToBegin( );
258   iIt.GoToBegin( );
259   oIt.GoToBegin( );
260   while( !( lIt.IsAtEnd( ) || iIt.IsAtEnd( ) || oIt.IsAtEnd( ) ) )
261   {
262     if( lIt.Get( ) != this->m_InsideValue )
263     {
264       if( this->m_UpperThreshold < iIt.Get( ) )
265         oIt.Set( this->m_OutsideLabel );
266       else
267         oIt.Set( TOutputValue( 0 ) );
268     }
269     else
270       oIt.Set( this->m_InsideLabel );
271     ++lIt;
272     ++iIt;
273     ++oIt;
274
275   } // elihw
276 }
277 */
278
279 #endif // __CTBronchi__MoriLabelling__hxx__
280
281 // eof - $RCSfile$