]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/RandomWalker.hxx
...
[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 typename fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar >::
65 TNodes fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar >::
66 _UnifySeeds( )
67 {
68   this->m_Seeds.clear( );
69   const TLabelImage* lbl = this->GetLabels( );
70   if( lbl == NULL )
71   {
72     std::ostringstream msg;
73     msg << "itk::ERROR: fpa::Image::RandomWalker (" << this
74         << "): Labelled image not defined.";
75     ::itk::ExceptionObject e(
76       __FILE__, __LINE__, msg.str( ).c_str( ), ITK_LOCATION
77       );
78     throw e;
79
80   } // fi
81
82   // Iterate over labels
83   typename TLabelImage::RegionType reg = lbl->GetRequestedRegion( );
84   itk::ImageRegionConstIteratorWithIndex< TLabelImage > lIt( lbl, reg );
85   for( lIt.GoToBegin( ); !lIt.IsAtEnd( ); ++lIt )
86   {
87     if( lIt.Get( ) > 0 )
88     {
89       bool is_seed = false;
90       for( unsigned int d = 0; d < TLabelImage::ImageDimension; ++d )
91       {
92         for( int s = -1; s <= 1; s += 2 )
93         {
94           TVertex neigh = lIt.GetIndex( );
95           neigh[ d ] += s;
96           is_seed |= ( lbl->GetPixel( neigh ) == 0 );
97
98         } // rof
99
100       } // rof
101
102       if( !is_seed )
103       {
104         typename TSeedsInterface::TNode node;
105         node.Vertex = lIt.GetIndex( );
106         node.Parent = lIt.GetIndex( );
107         node.FrontId = lIt.Get( );
108         node.Value = TScalar( 0 );
109         this->_Mark( node.Vertex, node.FrontId );
110         this->_UpdateOutputValue( node );
111       }
112       else
113       {
114         typename TSeedsInterface::TSeed seed;
115         seed.Vertex = lIt.GetIndex( );
116         seed.IsPoint = false;
117         seed.FrontId = lIt.Get( );
118         this->m_Seeds.push_back( seed );
119
120       } // fi
121
122     } // fi
123
124   } // rof
125
126   // Ok, finish initialization
127   return( this->Superclass::_UnifySeeds( ) );
128 }
129
130 #endif // __fpa__Image__RandomWalker__hxx__
131
132 // eof - $RCSfile$