]> Creatis software - FrontAlgorithms.git/blob - tests/image/RandomWalker/Gaussian.cxx
...
[FrontAlgorithms.git] / tests / image / RandomWalker / Gaussian.cxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #include <itkImage.h>
7 #include <itkImageFileReader.h>
8 #include <itkImageFileWriter.h>
9 #include <fpa/Filters/Image/RandomWalker.h>
10 #include <fpa/Functors/Dijkstra/Image/Gaussian.h>
11
12 // -------------------------------------------------------------------------
13 const unsigned int Dim = 2;
14 typedef unsigned char TInputPixel;
15 typedef unsigned char TInputLabel;
16 typedef float         TOutputPixel;
17 typedef itk::Image< TInputPixel, Dim >  TInputImage;
18 typedef itk::Image< TInputLabel, Dim >  TInputLabelImage;
19 typedef itk::Image< TOutputPixel, Dim > TOutputImage;
20
21 // -------------------------------------------------------------------------
22 int main( int argc, char* argv[] )
23 {
24   // Get arguments
25   if( argc < 7 )
26   {
27     std::cerr
28       << "Usage: " << argv[ 0 ]
29       << " input_image input_labels output_image output_costs alpha beta"
30       << std::endl;
31     return( 1 );
32
33   } // fi
34   std::string input_image_filename = argv[ 1 ];
35   std::string input_labels_filename = argv[ 2 ];
36   std::string output_image_filename = argv[ 3 ];
37   std::string output_costs_filename = argv[ 4 ];
38   double alpha = std::atof( argv[ 5 ] );
39   double beta = std::atof( argv[ 6 ] );
40
41   // Read image
42   typedef itk::ImageFileReader< TInputImage > TInputImageReader;
43   TInputImageReader::Pointer input_image_reader = TInputImageReader::New( );
44   input_image_reader->SetFileName( input_image_filename );
45
46   // Read labels
47   typedef itk::ImageFileReader< TInputLabelImage > TInputLabelImageReader;
48   TInputLabelImageReader::Pointer input_labels_reader =
49     TInputLabelImageReader::New( );
50   input_labels_reader->SetFileName( input_labels_filename );
51
52   // Prepare weight functor
53   typedef fpa::Functors::Dijkstra::Image::Gaussian< TInputImage, TOutputPixel > TWeight;
54   TWeight::Pointer weight = TWeight::New( );
55   weight->SetAlpha( alpha );
56   weight->SetBeta( beta );
57
58   // Prepare filter
59   typedef fpa::Filters::Image::RandomWalker< TInputImage, TInputLabelImage, TOutputImage > TFilter;
60   TFilter::Pointer filter = TFilter::New( );
61   filter->SetInputImage( input_image_reader->GetOutput( ) );
62   filter->SetInputLabels( input_labels_reader->GetOutput( ) );
63   filter->SetWeightFunction( weight );
64
65   // Execute filter
66   filter->Update( );
67
68   // Save results
69   typedef itk::ImageFileWriter< TFilter::TOutputLabels > TOutputWriter;
70   TOutputWriter::Pointer output_writer = TOutputWriter::New( );
71   output_writer->SetInput( filter->GetOutputLabels( ) );
72   output_writer->SetFileName( output_image_filename );
73
74   typedef itk::ImageFileWriter< TFilter::TCostsImage > TCostsWriter;
75   TCostsWriter::Pointer costs_writer = TCostsWriter::New( );
76   costs_writer->SetInput( filter->GetOutputCosts( ) );
77   costs_writer->SetFileName( output_costs_filename );
78
79   try
80   {
81     output_writer->Update( );
82     costs_writer->Update( );
83   }
84   catch( std::exception& err )
85   {
86     std::cerr << "Error caught: " << err.what( ) << std::endl;
87     return( 1 );
88
89   } // yrt
90
91   return( 0 );
92 }
93
94 // eof - $RCSfile$