1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
6 #ifndef __fpa__Image__RegionGrow__hxx__
7 #define __fpa__Image__RegionGrow__hxx__
11 // -------------------------------------------------------------------------
12 template< class _TInputImage, class _TOutputImage >
13 void fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
14 SetPredicate( TIntensityFunctor* functor )
16 if( this->m_IntensityFunctor.GetPointer( ) != functor )
18 this->m_IntensityFunctor = functor;
24 // -------------------------------------------------------------------------
25 template< class _TInputImage, class _TOutputImage >
26 void fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
27 AddSeed( const TIndex& seed )
29 if( this->m_Seeds.insert( seed ).second )
33 // -------------------------------------------------------------------------
34 template< class _TInputImage, class _TOutputImage >
35 fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
38 m_InsideValue( TInputPixel( 0 ) ),
39 m_OutsideValue( TInputPixel( 0 ) )
43 // -------------------------------------------------------------------------
44 template< class _TInputImage, class _TOutputImage >
45 fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
50 // -------------------------------------------------------------------------
51 template< class _TInputImage, class _TOutputImage >
52 void fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
53 GenerateInputRequestedRegion( )
55 this->Superclass::GenerateInputRequestedRegion( );
56 if( this->GetInput( ) )
59 const_cast< TInputImage* >( this->GetInput( ) );
60 input->SetRequestedRegionToLargestPossibleRegion( );
65 // -------------------------------------------------------------------------
66 template< class _TInputImage, class _TOutputImage >
67 void fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
68 EnlargeOutputRequestedRegion( itk::DataObject* output )
70 this->Superclass::EnlargeOutputRequestedRegion( output );
71 output->SetRequestedRegionToLargestPossibleRegion( );
74 // -------------------------------------------------------------------------
75 template< class _TInputImage, class _TOutputImage >
76 void fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
79 const TInputImage* input = this->GetInput( );
80 TOutputImage* output = this->GetOutput( );
81 TRegion region = input->GetRequestedRegion( );
84 output->SetBufferedRegion( region );
86 output->FillBuffer( this->m_OutsideValue );
89 typedef itk::Image< bool, TInputImage::ImageDimension > _TMarks;
90 typename _TMarks::Pointer marks = _TMarks::New( );
91 marks->CopyInformation( input );
92 marks->SetRequestedRegion( region );
93 marks->SetBufferedRegion( input->GetBufferedRegion( ) );
95 marks->FillBuffer( false );
98 std::queue< TIndex > q;
99 for( TIndex seed: this->m_Seeds )
103 while( q.size( ) > 0 )
105 // Get next candidate
106 TIndex node = q.front( );
108 if( marks->GetPixel( node ) )
110 marks->SetPixel( node, true );
112 // Apply inclusion predicate
113 TInputPixel value = input->GetPixel( node );
115 if( this->m_IntensityFunctor.IsNotNull( ) )
116 inside = this->m_IntensityFunctor->Evaluate( value );
120 // Ok, pixel lays inside region
121 output->SetPixel( node, this->m_InsideValue );
124 for( unsigned int d = 0; d < TInputImage::ImageDimension; ++d )
127 for( int i = -1; i <= 1; i += 2 )
129 neigh[ d ] = node[ d ] + i;
130 if( region.IsInside( neigh ) )
140 #endif // __fpa__Image__RegionGrow__hxx__