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__
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>
17 // -------------------------------------------------------------------------
18 template< class _TImage, class _TLabels, class _TScalarImage >
19 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
20 SliceBySliceRandomWalker( )
23 ivqITKInputConfigureMacro( InputLabels, TLabels );
24 ivqITKInputConfigureMacro( InputVesselness, TScalarImage );
27 // -------------------------------------------------------------------------
28 template< class _TImage, class _TLabels, class _TScalarImage >
29 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
30 ~SliceBySliceRandomWalker( )
34 // -------------------------------------------------------------------------
35 template< class _TImage, class _TLabels, class _TScalarImage >
37 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
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;
45 const TImage* input = this->GetInput( );
46 const TLabels* labels = this->GetInputLabels( );
47 const TScalarImage* vesselness = this->GetInputVesselness( );
48 this->AllocateOutputs( );
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 ];
56 this->m_VesselnessValue =
57 this->m_VesselnessThreshold * this->m_VesselnessMax / TScalar( 100 );
60 typename TScalarImage::Pointer composite;
61 this->_Composite( composite, labels, vesselness );
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 )
69 // Prepare extraction region
70 typename TImage::IndexType sliceIndex = regionIndex;
72 typename TImage::RegionType sliceRegion( sliceIndex, sliceSize );
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 );
82 // Update vectors with each image
83 data3D[ i ] = input_slice;
84 binaryTree[ i ] = labels_slice;
85 fusion[ i ] = composite_slice;
89 // Random walker slice-by-slice
90 this->_RandomWalker( binaryTree, data3D, fusion, true );
91 this->_RandomWalker( binaryTree, data3D, fusion, false );
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 ] );
99 this->GetOutput( )->Graft( join->GetOutput( ) );
102 // -------------------------------------------------------------------------
103 template< class _TImage, class _TLabels, class _TScalarImage >
105 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
107 typename TScalarImage::Pointer& composite,
108 const TLabels* labels,
109 const TScalarImage* vesselness
112 // Min-Max vesselness values
113 typedef itk::MinimumMaximumImageCalculator< TScalarImage > _TMinMax;
114 typename _TMinMax::Pointer minMax = _TMinMax::New( );
115 minMax->SetImage( vesselness );
117 TScalar maxVess = minMax->GetMaximum( );
118 typename TScalarImage::RegionType region = labels->GetRequestedRegion( );
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 );
131 for( ; !v.IsAtEnd( ); ++v, ++l, ++c )
132 c.Set( ( l.Get( ) != 0 )? maxVess: v.Get( ) );
135 // -------------------------------------------------------------------------
136 template< class _TImage, class _TLabels, class _TScalarImage >
137 template< class _TSlicePtr, class _TInput >
139 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
142 const _TInput* input,
143 typename _TInput::RegionType region
146 typedef typename _TSlicePTr::ObjectType _TSlice;
147 typedef itk::ExtractImageFilter< _TInput, _TSlice > _TExtract;
149 typename _TExtract::Pointer extract = _TExtract::New( );
150 extract->SetInput( input );
151 extract->SetExtractionRegion( region );
152 extract->SetDirectionCollapseToIdentity( );
154 slice = extract->GetOutput( );
155 slice->DisconnectPipeline( );
158 // -------------------------------------------------------------------------
159 template< class _TImage, class _TLabels, class _TScalarImage >
160 template< class _TBinaryTree, class _TData3D, class _TFusion >
162 fpa::Common::SliceBySliceRandomWalker< _TImage, _TLabels, _TScalarImage >::
164 _TBinaryTree& binaryTree,
165 const _TData3D& data3D,
166 const _TFusion& fusion,
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;
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 ) )
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( );
190 // Create seeds and background
191 _TSliceBinaryPtr seeds = _TSliceBinary::New( );
192 seeds->CopyInformation( tmp );
193 seeds->SetRequestedRegion( region );
195 seeds->FillBuffer( 0 );
197 itk::ImageRegionConstIterator< _TSliceBinary > tIt( tmp, region );
198 itk::ImageRegionConstIterator< _TSliceFusion > vIt( vess, region );
199 itk::ImageRegionIterator< _TSliceBinary > sIt( seeds, region );
206 for( ; !tIt.IsAtEnd( ); ++tIt, ++vIt, ++sIt )
208 if( tIt.Get( ) != 0 )
211 if( vIt.Get( ) > this->m_VesselnessValue )
220 if( vIt.Get( ) < this->m_VesselnessValue )
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( );
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 );
247 typedef itk::MaximumImageFilter< _TSliceBinary > _TMax;
248 _TMax::Pointer max = _TMax::New();
249 max->SetInput( 0, rw->GetOutput( ) );
250 max->SetInput( 1, next );
252 binaryTree[ z + o ] = max->GetOutput( );
253 binaryTree[ z + o ]->DisconnectPipeline( );
258 #endif // __fpa__Common__SliceBySliceRandomWalker__hxx__