]> Creatis software - FrontAlgorithms.git/blob - appli/CTBronchi/RandomWalker.cxx
...
[FrontAlgorithms.git] / appli / CTBronchi / RandomWalker.cxx
1 #include <chrono>
2 #include <itkImage.h>
3 #include <itkImageFileReader.h>
4 #include <itkImageFileWriter.h>
5
6 #include <fpa/Image/RandomWalker.h>
7 #include <fpa/Image/Functors/Dijkstra/Gaussian.h>
8
9 // -------------------------------------------------------------------------
10 const unsigned int Dim = 3;
11 typedef short          TPixel;
12 typedef unsigned short TLabel;
13 typedef double         TScalar;
14
15 typedef itk::Image< TPixel, Dim > TInputImage;
16 typedef itk::Image< TLabel, Dim > TLabelImage;
17
18 typedef fpa::Image::RandomWalker< TInputImage, TLabelImage, TScalar > TFilter;
19 typedef fpa::Image::Functors::Dijkstra::Gaussian< TInputImage, TScalar > TWeight;
20
21 // -------------------------------------------------------------------------
22 int main( int argc, char* argv[] )
23 {
24   // Get arguments
25   if( argc < 6 )
26   {
27     std::cerr
28       << "Usage: " << argv[ 0 ] << std::endl
29       << "   input_image label_image output_image" << std::endl
30       << "   alpha(0) beta(100)"
31       << std::endl;
32     return( 1 );
33
34   } // fi
35   std::string input_image_filename = argv[ 1 ];
36   std::string label_image_filename = argv[ 2 ];
37   std::string output_image_filename = argv[ 3 ];
38   double alpha = std::atof( argv[ 4 ] );
39   double beta = std::atof( argv[ 5 ] );
40
41   // Read images
42   itk::ImageFileReader< TInputImage >::Pointer input_image_reader =
43     itk::ImageFileReader< TInputImage >::New( );
44   input_image_reader->SetFileName( input_image_filename );
45
46   itk::ImageFileReader< TLabelImage >::Pointer label_image_reader =
47     itk::ImageFileReader< TLabelImage >::New( );
48   label_image_reader->SetFileName( label_image_filename );
49
50   // Prepare weight
51   TWeight::Pointer weight = TWeight::New( );
52   weight->SetAlpha( alpha );
53   weight->SetBeta( beta );
54
55   // Prepare filter
56   TFilter::Pointer filter = TFilter::New( );
57   filter->SetInput( input_image_reader->GetOutput( ) );
58   filter->SetLabels( label_image_reader->GetOutput( ) );
59   filter->SetWeightFunction( weight );
60
61   // Show some information
62   std::cout << "----------------------------------------------" << std::endl;
63   std::cout << "Image: " << input_image_filename << std::endl;
64
65   // Execute pipeline
66   std::chrono::time_point< std::chrono::high_resolution_clock > ts, te;
67   std::chrono::duration< double > tr;
68   try
69   {
70     ts = std::chrono::high_resolution_clock::now( );
71     input_image_reader->Update( );
72     te = std::chrono::high_resolution_clock::now( );
73     tr = te - ts;
74     std::cout << "Raw read time: " << tr.count( ) << " s" << std::endl;
75
76     ts = std::chrono::high_resolution_clock::now( );
77     label_image_reader->Update( );
78     te = std::chrono::high_resolution_clock::now( );
79     tr = te - ts;
80     std::cout << "Label read time: " << tr.count( ) << " s" << std::endl;
81
82     ts = std::chrono::high_resolution_clock::now( );
83     filter->Update( );
84     te = std::chrono::high_resolution_clock::now( );
85     tr = te - ts;
86     std::cout
87       << "Labelling time: " << tr.count( ) << " s" << std::endl;
88   }
89   catch( std::exception& err )
90   {
91     std::cerr << "Error caught: " << err.what( ) << std::endl;
92     return( 1 );
93
94   } // yrt
95
96   // Save output image
97   itk::ImageFileWriter< TLabelImage >::Pointer output_image_writer =
98     itk::ImageFileWriter< TLabelImage >::New( );
99   output_image_writer->SetInput( filter->GetMarks( ) );
100   output_image_writer->SetFileName( output_image_filename );
101   try
102   {
103     ts = std::chrono::high_resolution_clock::now( );
104     output_image_writer->Update( );
105     te = std::chrono::high_resolution_clock::now( );
106     tr = te - ts;
107     std::cout << "Write time: " << tr.count( ) << " s" << std::endl;
108   }
109   catch( std::exception& err )
110   {
111     std::cerr << "Error caught: " << err.what( ) << std::endl;
112     return( 1 );
113
114   } // yrt
115   std::cout << "----------------------------------------------" << std::endl;
116
117   return( 0 );
118 }
119
120 // eof - $RCSfile$