]> Creatis software - FrontAlgorithms.git/blob - libs/fpa/Image/RegionGrow.hxx
...
[FrontAlgorithms.git] / libs / fpa / Image / RegionGrow.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #ifndef __fpa__Image__RegionGrow__hxx__
7 #define __fpa__Image__RegionGrow__hxx__
8
9 #include <queue>
10
11 // -------------------------------------------------------------------------
12 template< class _TInputImage, class _TOutputImage >
13 void fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
14 SetPredicate( TIntensityFunctor* functor )
15 {
16   if( this->m_IntensityFunctor.GetPointer( ) != functor )
17   {
18     this->m_IntensityFunctor = functor;
19     this->Modified( );
20
21   } // fi
22 }
23
24 // -------------------------------------------------------------------------
25 template< class _TInputImage, class _TOutputImage >
26 fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
27 RegionGrow( )
28   : Superclass( ),
29     TSeedsInterface( this ),
30     m_InsideValue( TInputPixel( 0 ) ),
31     m_OutsideValue( TInputPixel( 0 ) )
32 {
33 }
34
35 // -------------------------------------------------------------------------
36 template< class _TInputImage, class _TOutputImage >
37 fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
38 ~RegionGrow( )
39 {
40 }
41
42 // -------------------------------------------------------------------------
43 template< class _TInputImage, class _TOutputImage >
44 void fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
45 GenerateInputRequestedRegion( )
46 {
47   this->Superclass::GenerateInputRequestedRegion( );
48   if( this->GetInput( ) )
49   {
50     TInputImage* input =
51       const_cast< TInputImage* >( this->GetInput( ) );
52     input->SetRequestedRegionToLargestPossibleRegion( );
53
54   } // fi
55 }
56
57 // -------------------------------------------------------------------------
58 template< class _TInputImage, class _TOutputImage >
59 void fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
60 EnlargeOutputRequestedRegion( itk::DataObject* output )
61 {
62   this->Superclass::EnlargeOutputRequestedRegion( output );
63   output->SetRequestedRegionToLargestPossibleRegion( );
64 }
65
66 // -------------------------------------------------------------------------
67 template< class _TInputImage, class _TOutputImage >
68 void fpa::Image::RegionGrow< _TInputImage, _TOutputImage >::
69 GenerateData( )
70 {
71   const TInputImage* input = this->GetInput( );
72   TOutputImage* output = this->GetOutput( );
73   TRegion region = input->GetRequestedRegion( );
74
75   // Configure output
76   output->SetBufferedRegion( region );
77   output->Allocate( );
78   output->FillBuffer( this->m_OutsideValue );
79
80   // Init marks
81   typedef itk::Image< bool, TInputImage::ImageDimension > _TMarks;
82   typename _TMarks::Pointer marks = _TMarks::New( );
83   marks->CopyInformation( input );
84   marks->SetRequestedRegion( region );
85   marks->SetBufferedRegion( input->GetBufferedRegion( ) );
86   marks->Allocate( );
87   marks->FillBuffer( false );
88
89   // Init queue
90   std::queue< TIndex > q;
91   for( TIndex seed: this->GetSeeds( ) )
92     q.push( seed );
93
94   // Main loop
95   while( q.size( ) > 0 )
96   {
97     // Get next candidate
98     TIndex node = q.front( );
99     q.pop( );
100     if( marks->GetPixel( node ) )
101       continue;
102     marks->SetPixel( node, true );
103
104     // Apply inclusion predicate
105     TInputPixel value = input->GetPixel( node );
106     bool inside = false;
107     if( this->m_IntensityFunctor.IsNotNull( ) )
108       inside = this->m_IntensityFunctor->Evaluate( value );
109     if( !inside )
110       continue;
111
112     // Ok, pixel lays inside region
113     output->SetPixel( node, this->m_InsideValue );
114
115     // Add neighborhood
116     for( unsigned int d = 0; d < TInputImage::ImageDimension; ++d )
117     {
118       TIndex neigh = node;
119       for( int i = -1; i <= 1; i += 2 )
120       {
121         neigh[ d ] = node[ d ] + i;
122         if( region.IsInside( neigh ) )
123           q.push( neigh );
124
125       } // rof
126
127     } // rof
128
129   } // elihw
130 }
131
132 #endif // __fpa__Image__RegionGrow__hxx__
133
134 // eof - $RCSfile$