]> Creatis software - FrontAlgorithms.git/blobdiff - tests/image/RandomWalker.cxx
...
[FrontAlgorithms.git] / tests / image / RandomWalker.cxx
diff --git a/tests/image/RandomWalker.cxx b/tests/image/RandomWalker.cxx
new file mode 100644 (file)
index 0000000..59c9105
--- /dev/null
@@ -0,0 +1,113 @@
+#include "BaseFunctions.h"
+#include <itkImage.h>
+#include <fpa/Image/RandomWalker.h>
+#include <fpa/Image/Functors/Dijkstra/Gaussian.h>
+
+// -------------------------------------------------------------------------
+const unsigned int Dim = 2;
+typedef short         TPixel;
+typedef unsigned char TLabel;
+typedef float         TScalar;
+
+typedef itk::Image< TPixel, Dim >                                    TInputImage;
+typedef itk::Image< TLabel, Dim >                                    TLabelImage;
+typedef fpa::Image::RandomWalker< TInputImage, TLabelImage, TScalar >    TFilter;
+typedef fpa::Image::Functors::Dijkstra::Gaussian< TInputImage, TScalar > TWeight;
+
+// -------------------------------------------------------------------------
+int main( int argc, char* argv[] )
+{
+  // Get arguments
+  if( argc < 7 )
+  {
+    std::cerr
+      << "Usage: " << argv[ 0 ]
+      << " input_image [labels_image] output_image output_costs"
+      << " alpha beta visual_debug"
+      << std::endl;
+    return( 1 );
+
+  } // fi
+  int idx = ( argc == 7 )? 2: 3;
+  std::string input_image_filename = argv[ 1 ];
+  std::string labels_image_filename = ( argc == 8 )? argv[ 2 ]: "";
+  std::string output_image_filename = argv[ idx ];
+  std::string output_costs_filename = argv[ idx + 1 ];
+  double alpha = std::atof( argv[ idx + 2 ] );
+  double beta = std::atof( argv[ idx + 3 ] );
+  bool visual_debug = ( argv[ idx + 4 ][ 0 ] == '1' );
+
+  // Read image
+  TInputImage::Pointer image;
+  std::string err0 = fpa::tests::image::Read( image, input_image_filename );
+  if( err0 != "" )
+  {
+    std::cerr << "Error caught: " << err0 << std::endl;
+    return( 1 );
+
+  } // fi
+
+  // Read label
+  TLabelImage::Pointer labels;
+  if( labels_image_filename != "" )
+  {
+    std::string err1 = fpa::tests::image::Read( labels, labels_image_filename );
+    if( err1 != "" )
+    {
+      std::cerr << "Error caught: " << err1 << std::endl;
+      return( 1 );
+
+    } // fi
+
+  } // fi
+
+  // Interact with image
+  fpa::tests::image::Viewer< TFilter > viewer( image );
+  if( visual_debug )
+  {
+    if( labels.IsNull( ) )
+      viewer.ActivateBrushWidget( );
+    viewer.Show( );
+
+  } // fi
+
+  // Prepare weight
+  TWeight::Pointer weight = TWeight::New( );
+  weight->SetAlpha( alpha );
+  weight->SetBeta( beta );
+
+  // Prepare filter
+  TFilter::Pointer filter = TFilter::New( );
+  filter->SetInput( image );
+  filter->SetWeightFunction( weight );
+  if( labels.IsNull( ) )
+    viewer.AssociateLabelsTo( filter );
+  else
+    filter->SetLabels( labels );
+
+  // Prepare visual debug and update
+  if( visual_debug )
+    viewer.ObserveFilter( filter );
+  try
+  {
+    filter->Update( );
+  }
+  catch( std::exception& err )
+  {
+    std::cerr << "Error caught: " << err.what( ) << std::endl;
+    return( 1 );
+
+  } // yrt
+
+  // Save results
+  std::string err1 =
+    fpa::tests::image::Write( filter->GetMarks( ), output_image_filename );
+  std::string err2 =
+    fpa::tests::image::Write( filter->GetOutput( ), output_costs_filename );
+  if( err1 != "" ) std::cerr << "Error caught: " << err1 << std::endl;
+  if( err2 != "" ) std::cerr << "Error caught: " << err2 << std::endl;
+
+  return( 0 );
+}
+
+// eof - $RCSfile$