]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Common/SliceBySliceRandomWalker.hxx
...
[FrontAlgorithms.git] / lib / fpa / Common / SliceBySliceRandomWalker.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #ifndef __fpa__Common__SliceBySliceRandomWalker__hxx__
6 #define __fpa__Common__SliceBySliceRandomWalker__hxx__
7
8 #include <vector>
9 #include <itkExtractImageFilter.h>
10 #include <itkImageRegionConstIterator.h>
11 #include <itkImageRegionIterator.h>
12 #include <itkJoinSeriesImageFilter.h>
13 #include <itkMinimumMaximumImageCalculator.h>
14 #include <fpa/Common/RandomWalker.h>
15 #include <fpa/Functors/Dijkstra/Image/Gaussian.h>
16
17 // -------------------------------------------------------------------------
18 template< class _TImage, class _TLabels, class _TScalarImage >
19 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
20 SliceBySliceRandomWalker( )
21   : Superclass( )
22 {
23   ivqITKInputConfigureMacro( InputLabels, TLabels );
24   ivqITKInputConfigureMacro( InputVesselness, TScalarImage );
25 }
26
27 // -------------------------------------------------------------------------
28 template< class _TImage, class _TLabels, class _TScalarImage >
29 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
30 ~SliceBySliceRandomWalker( )
31 {
32 }
33
34 // -------------------------------------------------------------------------
35 template< class _TImage, class _TLabels, class _TScalarImage >
36 void
37 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
38 GenerateData( )
39 {
40   typedef itk::Image< TPixel, TImage::ImageDimension - 1 > _TSliceImage;
41   typedef itk::Image< TLabel, TImage::ImageDimension - 1 > _TSliceLabels;
42   typedef itk::Image< TScalar, TImage::ImageDimension - 1 > _TSliceScalarImage;
43
44   // Preare I/O objects
45   const TImage* input = this->GetInput( );
46   const TLabels* labels = this->GetInputLabels( );
47   const TScalarImage* vesselness = this->GetInputVesselness( );
48   this->AllocateOutputs( );
49
50   // Some values
51   typename TImage::RegionType region = input->GetRequestedRegion( );
52   typename TImage::SizeType regionIndex = region.GetIndex( );
53   typename TImage::SizeType sliceSize = region.GetSize( );
54   int numSlices = sliceSize[ 2 ];
55   sliceSize[ 2 ] = 0;
56   this->m_VesselnessValue =
57     this->m_VesselnessThreshold * this->m_VesselnessMax / TScalar( 100 );
58
59   // Composite image
60   typename TScalarImage::Pointer composite;
61   this->_Composite( composite, labels, vesselness );
62
63   // Extract slices
64   std::vector< typename _TSliceImage::Pointer > data3D( numSlices );
65   std::vector< typename _TSliceLabels::Pointer > binaryTree( numSlices );
66   std::vector< typename _TSliceScalarImage::Pointer > fusion( numSlices );
67   for( int i = 0; i < numSlices; ++i )
68   {
69     // Prepare extraction region
70     typename TImage::IndexType sliceIndex = regionIndex;
71     sliceIndex[ 2 ] += i;
72     typename TImage::RegionType sliceRegion( sliceIndex, sliceSize );
73
74     // Extract regions
75     typename _TSliceImage::Pointer input_slice;
76     typename _TSliceLabels::Pointer labels_slice;
77     typename _TSliceScalarImage::Pointer composite_slice;
78     this->_Slice( input_slice, input, sliceRegion );
79     this->_Slice( labels_slice, labels, sliceRegion );
80     this->_Slice( composite_slice, composite, sliceRegion );
81
82     // Update vectors with each image
83     data3D[ i ] = input_slice;
84     binaryTree[ i ] = labels_slice;
85     fusion[ i ] = composite_slice;
86
87   } // rof
88
89   // Random walker slice-by-slice
90   this->_RandomWalker( binaryTree, data3D, fusion, true );
91   this->_RandomWalker( binaryTree, data3D, fusion, false );
92
93   // Join results
94   typedef itk::JoinSeriesImageFilter< _TSliceImage, TImage > _TJoin;
95   typename _TJoin::Pointer join = _TJoin::New( );
96   for( int i = 0; i < numSlices; ++i )
97     join->SetInput( i, binaryTree[ i ] );
98   join->Update( );
99   this->GetOutput( )->Graft( join->GetOutput( ) );
100 }
101
102 // -------------------------------------------------------------------------
103 template< class _TImage, class _TLabels, class _TScalarImage >
104 void
105 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
106 _Composite(
107   typename TScalarImage::Pointer& composite,
108   const TLabels* labels,
109   const TScalarImage* vesselness
110   )
111 {
112   // Min-Max vesselness values
113   typedef itk::MinimumMaximumImageCalculator< TScalarImage > _TMinMax;
114   typename _TMinMax::Pointer minMax = _TMinMax::New( );
115   minMax->SetImage( vesselness );
116   minMax->Compute( );
117   TScalar maxVess = minMax->GetMaximum( );
118   typename TScalarImage::RegionType region = labels->GetRequestedRegion( );
119
120   // Composite image
121   composite = TScalarImage::New( );
122   composite->CopyInformation( vesselness );
123   composite->SetBufferedRegion( vesselness->GetBufferedRegion( ) );
124   composite->Allocate( );
125   itk::ImageRegionConstIterator< TScalarImage > v( vesselness, region );
126   itk::ImageRegionConstIterator< TLabels > l( labels, region );
127   itk::ImageRegionIterator< TScalarImage > c( composite, region );
128   v.GoToBegin( );
129   l.GoToBegin( );
130   c.GoToBegin( );
131   for( ; !v.IsAtEnd( ); ++v, ++l, ++c )
132     c.Set( ( l.Get( ) != 0 )? maxVess: v.Get( ) );
133 }
134
135 // -------------------------------------------------------------------------
136 template< class _TImage, class _TLabels, class _TScalarImage >
137 template< class _TSlicePtr, class _TInput >
138 void
139 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
140 _Slice(
141   _TSlicePtr& slice,
142   const _TInput* input,
143   typename _TInput::RegionType region
144   )
145 {
146   typedef typename _TSlicePTr::ObjectType _TSlice;
147   typedef itk::ExtractImageFilter< _TInput, _TSlice > _TExtract;
148
149   typename _TExtract::Pointer extract = _TExtract::New( );
150   extract->SetInput( input );
151   extract->SetExtractionRegion( region );
152   extract->SetDirectionCollapseToIdentity( );
153   extract->Update( );
154   slice = extract->GetOutput( );
155   slice->DisconnectPipeline( );
156 }
157
158 // -------------------------------------------------------------------------
159 template< class _TImage, class _TLabels, class _TScalarImage >
160 template< class _TBinaryTree, class _TData3D, class _TFusion >
161 void
162 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
163 _RandomWalker(
164   _TBinaryTree& binaryTree,
165   const _TData3D& data3D,
166   const _TFusion& fusion,
167   bool down
168   )
169 {
170   typedef typename _TBinaryTree::value_type     _TSliceBinaryPtr;
171   typedef typename _TData3D::value_type         _TSliceData3DPtr;
172   typedef typename _TFusion::value_type         _TSliceFusionPtr;
173   typedef typename _TSliceBinaryPtr::ObjectType _TSliceBinary;
174   typedef typename _TSliceData3DPtr::ObjectType _TSliceData3D;
175   typedef typename _TSliceFusionPtr::ObjectType _TSliceFusion;
176   typedef typename _TSliceFusion::PixelType     _TSliceScalar;
177
178   int numSlices = binaryTree.size( );
179   int z = ( down )? -1: numSlices;
180   int o = ( down )? 1: -1;
181   while( ( down && z < numSlices - 2 ) || ( !down && z > 1 ) )
182   {
183     z += o;
184     _TSliceBinaryPtr tmp = binaryTree[ z ];
185     _TSliceBinaryPtr next = binaryTree[ z + o ];
186     _TSliceData3DPtr data = data3D[ z + o ];
187     _TSliceFusionPtr vess = fusion[ z + o ];
188     typename _TSliceBinary::RegionType region = tmp->GetRequestedRegion( );
189
190     // Create seeds and background
191     _TSliceBinaryPtr seeds = _TSliceBinary::New( );
192     seeds->CopyInformation( tmp );
193     seeds->SetRequestedRegion( region );
194     seeds->Allocate( );
195     seeds->FillBuffer( 0 );
196
197     itk::ImageRegionConstIterator< _TSliceBinary > tIt( tmp, region );
198     itk::ImageRegionConstIterator< _TSliceFusion > vIt( vess, region );
199     itk::ImageRegionIterator< _TSliceBinary > sIt( seeds, region );
200     tIt.GoToBegin( );
201     vIt.GoToBegin( );
202     sIt.GoToBegin( );
203     int len = 0;
204     int numO = 0;
205     int numB = 0;
206     for( ; !tIt.IsAtEnd( ); ++tIt, ++vIt, ++sIt )
207     {
208       if( tIt.Get( ) != 0 )
209       {
210         len++;
211         if( vIt.Get( ) > this->m_VesselnessValue )
212         {
213           sIt.Set( 1 );
214           numO++;
215
216         } // fi
217
218       } // fi
219
220       if( vIt.Get( ) < this->m_VesselnessValue )
221       {
222         sIt.Set( 2 );
223         numB++;
224
225       } // fi
226
227     } // rof
228
229     if( len == 0 )
230       continue;
231
232     // Random walker function
233     typedef fpa::Functors::Dijkstra::Image::Gaussian< _TSliceData3D, _TSliceScalar > _TFunction;
234     typename TFunction::Pointer edge = TFunction::New( );
235     edge->SetBeta( this->m_Beta );
236     edge->SetEpsilon( this->m_Eps );
237     edge->TreatAsWeightOff( );
238
239     // Real random walker
240     typedef fpa::Common::RandomWalker< _TSliceData3D, _TSliceBinary, _TSliceScalar > _TRandomWalker;
241     typename _TRandomWalker::Pointer rw = _TRandomWalker::New( );
242     rw->SetInput( data );
243     rw->SetInputLabels( seeds );
244     rw->SetEdgeFunction( edge );
245
246     // Keep maximum
247     typedef itk::MaximumImageFilter< _TSliceBinary > _TMax;
248     _TMax::Pointer max = _TMax::New();
249     max->SetInput( 0, rw->GetOutput( ) );
250     max->SetInput( 1, next );
251     max->Update( );
252     binaryTree[ z + o ] = max->GetOutput( );
253     binaryTree[ z + o ]->DisconnectPipeline( );
254
255   } // elihw
256 }
257
258 #endif // __fpa__Common__SliceBySliceRandomWalker__hxx__
259 // eof - $RCSfile$