]> Creatis software - FrontAlgorithms.git/blob - tests/image/Dijkstra/Invert.cxx
...
[FrontAlgorithms.git] / tests / image / Dijkstra / Invert.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/Dijkstra.h>
10 #include <fpa/Functors/Dijkstra/Invert.h>
11
12 // -------------------------------------------------------------------------
13 const unsigned int Dim = 2;
14 typedef unsigned char TInputPixel;
15 typedef float         TOutputPixel;
16 typedef itk::Image< TInputPixel, Dim >  TInputImage;
17 typedef itk::Image< TOutputPixel, Dim > TOutputImage;
18
19 // -------------------------------------------------------------------------
20 int main( int argc, char* argv[] )
21 {
22   // Get arguments
23   if( argc < 7 )
24   {
25     std::cerr
26       << "Usage: " << argv[ 0 ]
27       << " input_image output_image output_marks output_mst alpha beta [seeds]"
28       << std::endl;
29     return( 1 );
30
31   } // fi
32   std::string input_image_filename = argv[ 1 ];
33   std::string output_image_filename = argv[ 2 ];
34   std::string output_marks_filename = argv[ 3 ];
35   std::string output_mst_filename = argv[ 4 ];
36   double alpha = std::atof( argv[ 5 ] );
37   double beta = std::atof( argv[ 6 ] );
38
39   // Read image
40   typedef itk::ImageFileReader< TInputImage > TInputImageReader;
41   TInputImageReader::Pointer input_image_reader = TInputImageReader::New( );
42   input_image_reader->SetFileName( input_image_filename );
43
44   // Prepare weight functor
45   typedef fpa::Functors::Dijkstra::Invert< TOutputPixel > TWeight;
46   TWeight::Pointer weight = TWeight::New( );
47   weight->SetAlpha( alpha );
48   weight->SetBeta( beta );
49
50   // Prepare filter
51   typedef fpa::Filters::Image::Dijkstra< TInputImage, TOutputImage > TFilter;
52   TFilter::Pointer filter = TFilter::New( );
53   filter->SetInput( input_image_reader->GetOutput( ) );
54   filter->SetWeightFunction( weight );
55
56   // Get all seeds
57   for( int i = 7; i < argc; i += Dim )
58   {
59     TInputImage::IndexType seed;
60     for( int j = 0; j < Dim; ++j )
61       if( i + j < argc )
62         seed[ j ] = std::atoi( argv[ i + j ] );
63     filter->AddSeed( seed );
64
65   } // rof
66
67   // Execute filter
68   filter->Update( );
69
70   // Save results
71   typedef itk::ImageFileWriter< TFilter::TOutputImage > TOutputWriter;
72   TOutputWriter::Pointer output_writer = TOutputWriter::New( );
73   output_writer->SetInput( filter->GetOutput( ) );
74   output_writer->SetFileName( output_image_filename );
75
76   typedef itk::ImageFileWriter< TFilter::TMarksImage > TMarksWriter;
77   TMarksWriter::Pointer marks_writer = TMarksWriter::New( );
78   marks_writer->SetInput( filter->GetMarks( ) );
79   marks_writer->SetFileName( output_marks_filename );
80
81   typedef itk::ImageFileWriter< TFilter::TMST > TMSTWriter;
82   TMSTWriter::Pointer mst_writer = TMSTWriter::New( );
83   mst_writer->SetInput( filter->GetMinimumSpanningTree( ) );
84   mst_writer->SetFileName( output_mst_filename );
85
86   try
87   {
88     output_writer->Update( );
89     marks_writer->Update( );
90     mst_writer->Update( );
91   }
92   catch( std::exception& err )
93   {
94     std::cerr << "Error caught: " << err.what( ) << std::endl;
95     return( 1 );
96
97   } // yrt
98
99   return( 0 );
100 }
101
102 // eof - $RCSfile$