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 );
98 double t = MeasureTime( filter );
99 output_labels = filter->GetOutput( );
100 output_probabilities = filter->GetOutputProbabilities( );
101 output_labels->DisconnectPipeline( );
102 output_probabilities->DisconnectPipeline( );
103 std::cout << "Original RW executed in " << t << "s" << std::endl;
106 // -------------------------------------------------------------------------
107 template< class _TImagePtr, class _TLabelsPtr, class _TScalarImagePtr >
108 void FrontPropagation(
109 _TLabelsPtr& output_labels,
110 _TScalarImagePtr& output_probabilities,
111 const _TImagePtr& input,
112 const _TLabelsPtr& labels,
113 const typename _TScalarImagePtr::ObjectType::PixelType& beta,
114 const typename _TScalarImagePtr::ObjectType::PixelType& epsilon
117 typedef typename _TImagePtr::ObjectType _TImage;
118 typedef typename _TLabelsPtr::ObjectType _TLabels;
119 typedef typename _TScalarImagePtr::ObjectType _TScalarImage;
120 typedef typename _TScalarImage::PixelType _TScalar;
122 fpa::Functors::Dijkstra::Image::Gaussian< _TImage, _TScalar >
125 fpa::Filters::Image::RandomWalker< _TImage, _TLabels, _TScalar >
128 // Random walker function
129 typename _TFunction::Pointer edge = _TFunction::New( );
130 edge->SetBeta( beta );
131 edge->SetEpsilon( epsilon );
132 edge->TreatAsWeightOn( );
135 typename _TFilter::Pointer filter = _TFilter::New( );
136 filter->SetInputImage( input );
137 filter->SetInputLabels( labels );
138 filter->SetWeightFunction( edge );
139 double t = MeasureTime( filter );
140 output_labels = filter->GetOutputLabels( );
141 output_probabilities = filter->GetOutputCosts( );
142 output_labels->DisconnectPipeline( );
143 output_probabilities->DisconnectPipeline( );
144 std::cout << "Front propagation RW executed in " << t << "s" << std::endl;
147 // -------------------------------------------------------------------------
148 int main( int argc, char* argv[] )
150 // Get input arguments
154 << "Usage: " << argv[ 0 ]
155 << " input_image input_labels beta epsilon"
160 std::string input_image_filename = argv[ 1 ];
161 std::string input_labels_filename = argv[ 2 ];
162 std::istringstream values( std::string( argv[ 3 ] ) + " " + argv[ 4 ] );
163 TScalar beta, epsilon;
164 values >> beta >> epsilon;
167 TImage::Pointer input_image;
168 TLabels::Pointer input_labels;
169 ReadImage( input_image, input_image_filename );
170 ReadImage( input_labels, input_labels_filename );
172 // Execute algorithms
173 TLabels::Pointer original_labels, fp_labels;
174 TScalarImage::Pointer original_probabilities, fp_probabilities;
176 original_labels, original_probabilities,
177 input_image, input_labels,
181 fp_labels, fp_probabilities,
182 input_image, input_labels,
186 WriteImage( original_labels, "orig.png" );
187 WriteImage( fp_labels, "fp.png" );