1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
11 #include <itkImageFileReader.h>
12 #include <itkImageFileWriter.h>
14 #include <fpa/Common/OriginalRandomWalker.h>
15 #include <fpa/Filters/Image/RandomWalker.h>
16 #include <fpa/Functors/Dijkstra/Image/Gaussian.h>
18 // -------------------------------------------------------------------------
19 const unsigned int Dim = 3;
21 typedef unsigned char TLabel;
22 typedef double TScalar;
24 typedef itk::Image< TPixel, Dim > TImage;
25 typedef itk::Image< TLabel, Dim > TLabels;
26 typedef itk::Image< TScalar, Dim > TScalarImage;
28 // -------------------------------------------------------------------------
29 double MeasureTime( itk::ProcessObject* f )
31 std::chrono::time_point< std::chrono::high_resolution_clock > s, e;
32 std::chrono::duration< double > t;
33 s = std::chrono::high_resolution_clock::now( );
35 e = std::chrono::high_resolution_clock::now( );
40 // -------------------------------------------------------------------------
41 template< class _TImagePtr >
42 void ReadImage( _TImagePtr& image, const std::string& fname )
44 typedef itk::ImageFileReader< typename _TImagePtr::ObjectType > _TReader;
45 typename _TReader::Pointer r = _TReader::New( );
46 r->SetFileName( fname );
47 double t = MeasureTime( r );
48 image = r->GetOutput( );
49 image->DisconnectPipeline( );
50 std::cout << "Image \"" << fname << "\" read in " << t << "s" << std::endl;
53 // -------------------------------------------------------------------------
54 template< class _TImagePtr >
55 void WriteImage( const _TImagePtr& image, const std::string& fname )
57 typedef itk::ImageFileWriter< typename _TImagePtr::ObjectType > _TWriter;
58 typename _TWriter::Pointer w = _TWriter::New( );
60 w->SetFileName( fname );
61 double t = MeasureTime( w );
62 std::cout << "Image \"" << fname << "\" written in " << t << "s" << std::endl;
65 // -------------------------------------------------------------------------
66 template< class _TImagePtr, class _TLabelsPtr, class _TScalarImagePtr >
68 _TLabelsPtr& output_labels,
69 _TScalarImagePtr& output_probabilities,
70 const _TImagePtr& input,
71 const _TLabelsPtr& labels,
72 const typename _TScalarImagePtr::ObjectType::PixelType& beta,
73 const typename _TScalarImagePtr::ObjectType::PixelType& epsilon
76 typedef typename _TImagePtr::ObjectType _TImage;
77 typedef typename _TLabelsPtr::ObjectType _TLabels;
78 typedef typename _TScalarImagePtr::ObjectType _TScalarImage;
79 typedef typename _TScalarImage::PixelType _TScalar;
81 fpa::Functors::Dijkstra::Image::Gaussian< _TImage, _TScalar >
84 fpa::Common::OriginalRandomWalker< _TImage, _TLabels, _TScalar >
87 // Random walker function
88 typename _TFunction::Pointer edge = _TFunction::New( );
89 edge->SetBeta( beta );
90 edge->SetEpsilon( epsilon );
91 edge->TreatAsWeightOff( );
94 typename _TFilter::Pointer filter = _TFilter::New( );
95 filter->SetInput( input );
96 filter->SetInputLabels( labels );
97 filter->SetEdgeFunction( edge );
99 double t = MeasureTime( filter );
100 output_labels = filter->GetOutput( );
101 output_probabilities = filter->GetOutputProbabilities( );
102 output_labels->DisconnectPipeline( );
103 output_probabilities->DisconnectPipeline( );
104 std::cout << "Original RW executed in " << t << "s" << std::endl;
107 // -------------------------------------------------------------------------
108 template< class _TImagePtr, class _TLabelsPtr, class _TScalarImagePtr >
109 void FrontPropagation(
110 _TLabelsPtr& output_labels,
111 _TScalarImagePtr& output_probabilities,
112 const _TImagePtr& input,
113 const _TLabelsPtr& labels,
114 const typename _TScalarImagePtr::ObjectType::PixelType& beta,
115 const typename _TScalarImagePtr::ObjectType::PixelType& epsilon
118 typedef typename _TImagePtr::ObjectType _TImage;
119 typedef typename _TLabelsPtr::ObjectType _TLabels;
120 typedef typename _TScalarImagePtr::ObjectType _TScalarImage;
121 typedef typename _TScalarImage::PixelType _TScalar;
123 fpa::Functors::Dijkstra::Image::Gaussian< _TImage, _TScalar >
126 fpa::Filters::Image::RandomWalker< _TImage, _TLabels, _TScalar >
129 // Random walker function
130 typename _TFunction::Pointer edge = _TFunction::New( );
131 edge->SetBeta( beta );
132 edge->SetEpsilon( epsilon );
133 edge->TreatAsWeightOn( );
136 typename _TFilter::Pointer filter = _TFilter::New( );
137 filter->SetInputImage( input );
138 filter->SetInputLabels( labels );
139 filter->SetWeightFunction( edge );
140 double t = MeasureTime( filter );
141 output_labels = filter->GetOutputLabels( );
142 output_probabilities = filter->GetOutputCosts( );
143 output_labels->DisconnectPipeline( );
144 output_probabilities->DisconnectPipeline( );
145 std::cout << "Front propagation RW executed in " << t << "s" << std::endl;
148 // -------------------------------------------------------------------------
149 int main( int argc, char* argv[] )
151 // Get input arguments
155 << "Usage: " << argv[ 0 ]
156 << " input_image input_labels beta epsilon"
161 std::string input_image_filename = argv[ 1 ];
162 std::string input_labels_filename = argv[ 2 ];
163 std::istringstream values( std::string( argv[ 3 ] ) + " " + argv[ 4 ] );
164 TScalar beta, epsilon;
165 values >> beta >> epsilon;
168 TImage::Pointer input_image;
169 TLabels::Pointer input_labels;
170 ReadImage( input_image, input_image_filename );
171 ReadImage( input_labels, input_labels_filename );
173 // Execute algorithms
174 TLabels::Pointer original_labels, fp_labels;
175 TScalarImage::Pointer original_probabilities, fp_probabilities;
177 original_labels, original_probabilities,
178 input_image, input_labels,
182 fp_labels, fp_probabilities,
183 input_image, input_labels,
187 WriteImage( original_labels, "orig.mhd" );
188 WriteImage( fp_labels, "fp.mhd" );