]> Creatis software - FrontAlgorithms.git/blobdiff - appli/examples/example_ImageAlgorithmRegionGrow_MultipleThresholds.cxx
Some visualizaton added
[FrontAlgorithms.git] / appli / examples / example_ImageAlgorithmRegionGrow_MultipleThresholds.cxx
index 1f4bf3fa51f4204d927d155f148c37d93539c903..e9b06cc7238e475e0ac725120740153abc752cde 100644 (file)
@@ -1,3 +1,4 @@
+#include <ctime>
 #include <iostream>
 #include <limits>
 #include <set>
@@ -5,9 +6,11 @@
 
 #include <itkImage.h>
 #include <itkImageFileReader.h>
+#include <itkImageFileWriter.h>
 #include <itkImageToVTKImageFilter.h>
 
 #include <vtkImageActor.h>
+#include <vtkImageMarchingCubes.h>
 #include <vtkProperty.h>
 #include <vtkRenderer.h>
 #include <vtkRenderWindow.h>
@@ -27,6 +30,7 @@ typedef itk::Image< TPixel, Dim >  TImage;
 typedef itk::ImageToVTKImageFilter< TImage > TVTKImage;
 
 typedef itk::ImageFileReader< TImage >   TImageReader;
+typedef itk::ImageFileWriter< TImage >   TImageWriter;
 
 typedef
 fpa::Image::RegionGrowWithMultipleThresholds< TImage >
@@ -38,18 +42,24 @@ TObserver;
 // -------------------------------------------------------------------------
 int main( int argc, char* argv[] )
 {
-  if( argc < 5 )
+  if( argc < 6 )
   {
     std::cerr
       << "Usage: " << argv[ 0 ]
-      << " input_image thr_0 thr_1 n_samples" << std::endl;
+      << " input_image thr_0 thr_1 step output_image"
+      << " visual_debug"
+      << std::endl;
     return( 1 );
 
   } // fi
   std::string input_image_fn = argv[ 1 ];
   TPixel thr_0 = TPixel( std::atof( argv[ 2 ] ) );
   TPixel thr_1 = TPixel( std::atof( argv[ 3 ] ) );
-  unsigned int n_samples = std::atoi( argv[ 4 ] );
+  unsigned int step = std::atoi( argv[ 4 ] );
+  std::string output_image_fn = argv[ 5 ];
+  bool visual_debug = false;
+  if( argc > 6 )
+    visual_debug = ( std::atoi( argv[ 6 ] ) == 1 );
   
   // Read image
   TImageReader::Pointer input_image_reader = TImageReader::New( );
@@ -74,43 +84,82 @@ int main( int argc, char* argv[] )
   vtk_image->SetInput( input_image );
   vtk_image->Update( );
 
-  /* TODO
-     vtkSmartPointer< vtkSphereSource > seed =
-     vtkSmartPointer< vtkSphereSource >::New( );
-     seed->SetCenter( seed_pnt[ 0 ], seed_pnt[ 1 ], seed_pnt[ 2 ] );
-     seed->SetRadius( min_spac * double( 5 ) );
-     seed->Update( );
-  */
-
   fpa::VTK::ImageMPR view;
   view.SetBackground( 0.3, 0.2, 0.8 );
   view.SetSize( 800, 800 );
   view.SetImage( vtk_image->GetOutput( ) );
-  // TODO: view.AddPolyData( seed->GetOutput( ), 1, 0, 0 );
-  view.Start( );
-
-  // Configure observer
-  /* TODO
-     TObserver::Pointer obs = TObserver::New( );
-     obs->SetImage( input_image, view.GetWindow( ) );
-
-     // Configure algorithm
-     TFrontAlgorithm::Pointer algorithm = TFrontAlgorithm::New( );
-     algorithm->AddThresholds( thr_0, thr_1, n_samples );
 
-     TImage::IndexType seed_idx;
-     input_image->TransformPhysicalPointToIndex( seed_pnt, seed_idx );
-     algorithm->AddSeed( seed_idx, 0 );
+  // Wait for a seed to be given
+  while( view.GetNumberOfSeeds( ) == 0 )
+    view.Start( );
+
+  // Convert seed
+  double seed[ 3 ];
+  view.GetSeed( 0, seed );
+  TImage::PointType seed_pnt;
+  seed_pnt[ 0 ] = seed[ 0 ];
+  seed_pnt[ 1 ] = seed[ 1 ];
+  seed_pnt[ 2 ] = seed[ 2 ];
+  TImage::IndexType seed_idx;
+  input_image->TransformPhysicalPointToIndex( seed_pnt, seed_idx );
+
+  // Configure algorithm
+  TFrontAlgorithm::Pointer algorithm = TFrontAlgorithm::New( );
+  algorithm->AddThresholds( thr_0, thr_1, step );
+  algorithm->AddSeed( seed_idx, 0 );
+  algorithm->SetInput( input_image );
+  algorithm->SetNeighborhoodOrder( 1 );
+  algorithm->SetDifferenceThreshold( double( 3 ) );
+
+  if( visual_debug )
+  {
+    // Configure observer
+    TObserver::Pointer obs = TObserver::New( );
+    obs->SetImage( input_image, view.GetWindow( ) );
+    algorithm->AddObserver( itk::AnyEvent( ), obs );
+    algorithm->ThrowEventsOn( );
+  }
+  else
+    algorithm->ThrowEventsOff( );
+
+  std::clock_t start = std::clock( );
+  algorithm->Update( );
+  std::clock_t end = std::clock( );
+  double seconds = double( end - start ) / double( CLOCKS_PER_SEC );
+  std::cout << "Execution time = " << seconds << std::endl;
+
+  // Show result
+  TVTKImage::Pointer output_image_vtk = TVTKImage::New( );
+  output_image_vtk->SetInput( algorithm->GetOutput( ) );
+  output_image_vtk->Update( );
+
+  vtkSmartPointer< vtkImageMarchingCubes > mc =
+    vtkSmartPointer< vtkImageMarchingCubes >::New( );
+  mc->SetInputData( output_image_vtk->GetOutput( ) );
+  mc->SetValue(
+    0,
+    double( algorithm->GetInsideValue( ) ) * double( 0.95 )
+    );
+  mc->Update( );
+
+  // Let some interaction and close program
+  view.AddPolyData( mc->GetOutput( ), 0.1, 0.6, 0.8, 0.5 );
+  view.Start( );
 
-     algorithm->AddObserver( itk::AnyEvent( ), obs );
-     algorithm->ThrowEventsOn( );
-     algorithm->SetInput( input_image );
-     algorithm->SetNeighborhoodOrder( 1 );
-     algorithm->SetDerivativeThreshold( double( 3 ) );
-     algorithm->Update( );
+  // Write resulting image
+  TImageWriter::Pointer output_image_writer = TImageWriter::New( );
+  output_image_writer->SetInput( algorithm->GetOutput( ) );
+  output_image_writer->SetFileName( output_image_fn );
+  try
+  {
+    output_image_writer->Update( );
+  }
+  catch( itk::ExceptionObject& err )
+  {
+    std::cerr << "Error caught: " << err << std::endl;
+    return( 1 );
 
-     view.Start( );
-  */
+  } // yrt
   return( 0 );
 }