]> Creatis software - FrontAlgorithms.git/blob - tests/image/RandomWalker.cxx
59c91051b551aba4b93c2478868e38a19b26bcdf
[FrontAlgorithms.git] / tests / image / RandomWalker.cxx
1 #include "BaseFunctions.h"
2 #include <itkImage.h>
3 #include <fpa/Image/RandomWalker.h>
4 #include <fpa/Image/Functors/Dijkstra/Gaussian.h>
5
6 // -------------------------------------------------------------------------
7 const unsigned int Dim = 2;
8 typedef short         TPixel;
9 typedef unsigned char TLabel;
10 typedef float         TScalar;
11
12 typedef itk::Image< TPixel, Dim >                                    TInputImage;
13 typedef itk::Image< TLabel, Dim >                                    TLabelImage;
14 typedef fpa::Image::RandomWalker< TInputImage, TLabelImage, TScalar >    TFilter;
15 typedef fpa::Image::Functors::Dijkstra::Gaussian< TInputImage, TScalar > TWeight;
16
17 // -------------------------------------------------------------------------
18 int main( int argc, char* argv[] )
19 {
20   // Get arguments
21   if( argc < 7 )
22   {
23     std::cerr
24       << "Usage: " << argv[ 0 ]
25       << " input_image [labels_image] output_image output_costs"
26       << " alpha beta visual_debug"
27       << std::endl;
28     return( 1 );
29
30   } // fi
31   int idx = ( argc == 7 )? 2: 3;
32   std::string input_image_filename = argv[ 1 ];
33   std::string labels_image_filename = ( argc == 8 )? argv[ 2 ]: "";
34   std::string output_image_filename = argv[ idx ];
35   std::string output_costs_filename = argv[ idx + 1 ];
36   double alpha = std::atof( argv[ idx + 2 ] );
37   double beta = std::atof( argv[ idx + 3 ] );
38   bool visual_debug = ( argv[ idx + 4 ][ 0 ] == '1' );
39
40   // Read image
41   TInputImage::Pointer image;
42   std::string err0 = fpa::tests::image::Read( image, input_image_filename );
43   if( err0 != "" )
44   {
45     std::cerr << "Error caught: " << err0 << std::endl;
46     return( 1 );
47
48   } // fi
49
50   // Read label
51   TLabelImage::Pointer labels;
52   if( labels_image_filename != "" )
53   {
54     std::string err1 = fpa::tests::image::Read( labels, labels_image_filename );
55     if( err1 != "" )
56     {
57       std::cerr << "Error caught: " << err1 << std::endl;
58       return( 1 );
59
60     } // fi
61
62   } // fi
63
64   // Interact with image
65   fpa::tests::image::Viewer< TFilter > viewer( image );
66   if( visual_debug )
67   {
68     if( labels.IsNull( ) )
69       viewer.ActivateBrushWidget( );
70     viewer.Show( );
71
72   } // fi
73
74   // Prepare weight
75   TWeight::Pointer weight = TWeight::New( );
76   weight->SetAlpha( alpha );
77   weight->SetBeta( beta );
78
79   // Prepare filter
80   TFilter::Pointer filter = TFilter::New( );
81   filter->SetInput( image );
82   filter->SetWeightFunction( weight );
83   if( labels.IsNull( ) )
84     viewer.AssociateLabelsTo( filter );
85   else
86     filter->SetLabels( labels );
87
88   // Prepare visual debug and update
89   if( visual_debug )
90     viewer.ObserveFilter( filter );
91   try
92   {
93     filter->Update( );
94   }
95   catch( std::exception& err )
96   {
97     std::cerr << "Error caught: " << err.what( ) << std::endl;
98     return( 1 );
99
100   } // yrt
101
102   // Save results
103   std::string err1 =
104     fpa::tests::image::Write( filter->GetMarks( ), output_image_filename );
105   std::string err2 =
106     fpa::tests::image::Write( filter->GetOutput( ), output_costs_filename );
107   if( err1 != "" ) std::cerr << "Error caught: " << err1 << std::endl;
108   if( err2 != "" ) std::cerr << "Error caught: " << err2 << std::endl;
109
110   return( 0 );
111 }
112
113 // eof - $RCSfile$