1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
6 #ifndef __CTBronchi__MoriLabelling__hxx__
7 #define __CTBronchi__MoriLabelling__hxx__
9 #include <itkImageRegionConstIteratorWithIndex.h>
11 // -------------------------------------------------------------------------
12 template< class _TInputImage, class _TLabelImage >
13 const typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
14 TLabelImage* CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
15 GetInputLabelImage( ) const
17 return( this->GetLabels( ) );
20 // -------------------------------------------------------------------------
21 template< class _TInputImage, class _TLabelImage >
22 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
23 SetInputLabelImage( TLabelImage* image )
25 this->SetLabels( image );
28 // -------------------------------------------------------------------------
29 template< class _TInputImage, class _TLabelImage >
30 const typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
31 TInputImage* CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
32 GetInputRawImage( ) const
34 return( this->GetInput( ) );
37 // -------------------------------------------------------------------------
38 template< class _TInputImage, class _TLabelImage >
39 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
40 SetInputRawImage( TInputImage* image )
42 this->SetInput( image );
45 // -------------------------------------------------------------------------
46 template< class _TInputImage, class _TLabelImage >
47 typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
48 TInputValue CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
49 GetUpperThreshold( ) const
51 const TThresholdFunction* func =
52 dynamic_cast< const TThresholdFunction* >( this->GetValuePredicate( ) );
54 return( func->GetUpper( ) );
56 return( TInputValue( 0 ) );
59 // -------------------------------------------------------------------------
60 template< class _TInputImage, class _TLabelImage >
61 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
62 SetUpperThreshold( TInputValue t )
64 TThresholdFunction* func =
65 dynamic_cast< TThresholdFunction* >( this->GetValuePredicate( ) );
70 // -------------------------------------------------------------------------
71 template< class _TInputImage, class _TLabelImage >
72 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
75 m_InsideLabel( TOutputValue( 0 ) ),
76 m_OutsideLabel( TOutputValue( 0 ) )
78 typename TThresholdFunction::Pointer func = TThresholdFunction::New( );
79 this->SetPredicate( func );
82 // -------------------------------------------------------------------------
83 template< class _TInputImage, class _TLabelImage >
84 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
89 // -------------------------------------------------------------------------
90 template< class _TInputImage, class _TLabelImage >
91 typename CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
92 TNodes CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
95 this->m_Seeds.clear( );
96 const TLabelImage* lbl = this->GetLabels( );
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
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 )
116 bool is_seed = false;
117 for( unsigned int d = 0; d < TLabelImage::ImageDimension; ++d )
119 for( int s = -1; s <= 1; s += 2 )
121 TVertex neigh = lIt.GetIndex( );
123 if( reg.IsInside( neigh ) )
124 is_seed |= ( lbl->GetPixel( neigh ) == 0 );
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 );
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 );
154 // Ok, finish initialization
155 return( this->Superclass::_UnifySeeds( ) );
158 // -------------------------------------------------------------------------
159 template< class _TInputImage, class _TLabelImage >
160 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
161 _UpdateOutputValue( TNode& n )
163 const TInputImage* input = this->GetInputRawImage( );
164 const TLabelImage* labels = this->GetInputLabelImage( );
169 TOutputValue m_InsideValue;
170 TOutputValue m_InsideLabel;
171 TOutputValue m_OutsideLabel;
184 #include <itkImageRegionConstIterator.h>
185 #include <itkImageRegionIterator.h>
187 // -------------------------------------------------------------------------
188 template< class _TInputImage, class _TLabelImage >
190 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
191 GetInputLabelImage( ) const
193 return( this->GetInput( ) );
196 // -------------------------------------------------------------------------
197 template< class _TInputImage, class _TLabelImage >
198 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
199 SetInputLabelImage( TLabelImage* image )
201 this->SetInput( image );
204 // -------------------------------------------------------------------------
205 template< class _TInputImage, class _TLabelImage >
207 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
208 GetInputRawImage( ) const
211 dynamic_cast< const TInputImage* >(
212 this->itk::ProcessObject::GetInput( 1 )
217 // -------------------------------------------------------------------------
218 template< class _TInputImage, class _TLabelImage >
219 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
220 SetInputRawImage( TInputImage* image )
222 this->itk::ProcessObject::SetNthInput( 1, image );
225 // -------------------------------------------------------------------------
226 template< class _TInputImage, class _TLabelImage >
227 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
230 m_UpperThreshold( TOutputValue( 0 ) ),
231 m_InsideValue( TOutputValue( 0 ) ),
232 m_InsideLabel( TOutputValue( 0 ) ),
233 m_OutsideLabel( TOutputValue( 0 ) )
235 this->SetNumberOfRequiredInputs( 2 );
238 // -------------------------------------------------------------------------
239 template< class _TInputImage, class _TLabelImage >
240 CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
245 // -------------------------------------------------------------------------
246 template< class _TInputImage, class _TLabelImage >
247 void CTBronchi::MoriLabelling< _TInputImage, _TLabelImage >::
248 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType threadId )
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 );
260 while( !( lIt.IsAtEnd( ) || iIt.IsAtEnd( ) || oIt.IsAtEnd( ) ) )
262 if( lIt.Get( ) != this->m_InsideValue )
264 if( this->m_UpperThreshold < iIt.Get( ) )
265 oIt.Set( this->m_OutsideLabel );
267 oIt.Set( TOutputValue( 0 ) );
270 oIt.Set( this->m_InsideLabel );
279 #endif // __CTBronchi__MoriLabelling__hxx__