]> 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, class _TTraits >
18 itk::ModifiedTimeType
19 fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar, _TTraits >::
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, class _TTraits >
35 fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar, _TTraits >::
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, class _TTraits >
45 fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar, _TTraits >::
46 ~RandomWalker( )
47 {
48 }
49
50 // -------------------------------------------------------------------------
51 template< class _TInputImage, class _TLabelImage, class _TScalar, class _TTraits >
52 void fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar, _TTraits >::
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, class _TTraits >
64 typename fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar, _TTraits >::
65 TNodes fpa::Image::RandomWalker< _TInputImage, _TLabelImage, _TScalar, _TTraits >::
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           if( reg.IsInside( neigh ) )
97             is_seed |= ( lbl->GetPixel( neigh ) == 0 );
98
99         } // rof
100
101       } // rof
102
103       if( !is_seed )
104       {
105         TNode node;
106         node.Vertex = lIt.GetIndex( );
107         node.Parent = lIt.GetIndex( );
108         node.FrontId = lIt.Get( );
109         node.Value = TScalar( 0 );
110         this->_Mark( node.Vertex, node.FrontId );
111         this->_UpdateOutputValue( node );
112       }
113       else
114       {
115         TSeed seed;
116         seed.Vertex = lIt.GetIndex( );
117         seed.IsPoint = false;
118         seed.FrontId = lIt.Get( );
119         this->m_Seeds.push_back( seed );
120
121       } // fi
122
123     } // fi
124
125   } // rof
126
127   // Ok, finish initialization
128   return( this->Superclass::_UnifySeeds( ) );
129 }
130
131 #endif // __fpa__Image__RandomWalker__hxx__
132
133 // eof - $RCSfile$