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