]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/RandomWalker.hxx
9016e970e987d4a253deb5fd36caf7e1db335914
[FrontAlgorithms.git] / lib / fpa / Image / RandomWalker.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #ifndef __fpa__Image__RandomWalker__hxx__
7 #define __fpa__Image__RandomWalker__hxx__
8
9 #include <sstream>
10
11 #include <itkImageRegionConstIteratorWithIndex.h>
12 #include <itkExceptionObject.h>
13
14 #include <fpa/Image/Functors/Dijkstra/Gaussian.h>
15
16 // -------------------------------------------------------------------------
17 template< class _TInputImage, class _TLabelImage, class _TScalar >
18 itk::ModifiedTimeType
19 fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar >::
20 GetMTime( ) const
21 {
22   const TLabelImage* labels = this->GetLabels( );
23   itk::ModifiedTimeType t = this->Superclass::GetMTime( );
24   if( labels != NULL )
25   {
26     itk::ModifiedTimeType q = labels->GetMTime( );
27     t = ( q < t )? q: t;
28
29   } // fi
30   return( t );
31 }
32
33 // -------------------------------------------------------------------------
34 template< class _TInputImage, class _TLabelImage, class _TScalar >
35 fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar >::
36 RandomWalker( )
37   : Superclass( )
38 {
39   typedef fpa::Image::Functors::Dijkstra::Gaussian< TInputImage, TScalar > _TDefaultFunction;
40   this->SetWeightFunction( _TDefaultFunction::New( ) );
41 }
42
43 // -------------------------------------------------------------------------
44 template< class _TInputImage, class _TLabelImage, class _TScalar >
45 fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar >::
46 ~RandomWalker( )
47 {
48 }
49
50 // -------------------------------------------------------------------------
51 template< class _TInputImage, class _TLabelImage, class _TScalar >
52 void fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar >::
53 _BeforeGenerateData( )
54 {
55   this->Superclass::_BeforeGenerateData( );
56   TWeightFunction* wf =
57     dynamic_cast< TWeightFunction* >( this->GetWeightFunction( ) );
58   if( wf != NULL )
59     wf->SetImage( this->GetInput( ) );
60 }
61
62 // -------------------------------------------------------------------------
63 template< class _TInputImage, class _TLabelImage, class _TScalar >
64 void fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar >::
65 _QueueInit( )
66 {
67   this->m_Seeds.clear( );
68   const TLabelImage* lbl = this->GetLabels( );
69   if( lbl == NULL )
70   {
71     std::ostringstream msg;
72     msg << "itk::ERROR: fpa::Image::RandomWalker (" << this
73         << "): Labelled image not defined.";
74     ::itk::ExceptionObject e(
75       __FILE__, __LINE__, msg.str( ).c_str( ), ITK_LOCATION
76       );
77     throw e;
78
79   } // fi
80
81   // Iterate over labels
82   typename TLabelImage::RegionType reg = lbl->GetRequestedRegion( );
83   itk::ImageRegionConstIteratorWithIndex< TLabelImage > lIt( lbl, reg );
84   for( lIt.GoToBegin( ); !lIt.IsAtEnd( ); ++lIt )
85   {
86     if( lIt.Get( ) > 0 )
87     {
88       bool is_seed = false;
89       for( unsigned int d = 0; d < TLabelImage::ImageDimension; ++d )
90       {
91         for( int s = -1; s <= 1; s += 2 )
92         {
93           TVertex neigh = lIt.GetIndex( );
94           neigh[ d ] += s;
95           is_seed |= ( lbl->GetPixel( neigh ) == 0 );
96
97         } // rof
98
99       } // rof
100
101       typename TSeedsInterface::TNode node;
102       node.Vertex = lIt.GetIndex( );
103       node.Parent = lIt.GetIndex( );
104       node.FrontId = lIt.Get( );
105       node.Value = TScalar( 0 );
106       if( !is_seed )
107       {
108         this->_Mark( lIt.GetIndex( ), lIt.Get( ) );
109         this->_UpdateOutputValue( node );
110       }
111       else
112         this->m_Seeds.insert( node );
113
114     } // fi
115
116   } // rof
117
118   // Ok, finish initialization
119   this->Superclass::_QueueInit( );
120 }
121
122 #endif // __fpa__Image__RandomWalker__hxx__
123
124 // eof - $RCSfile$