]> Creatis software - FrontAlgorithms.git/blobdiff - examples/RegionGrow_Mori.cxx
...
[FrontAlgorithms.git] / examples / RegionGrow_Mori.cxx
index 7fd51b4427fd73942c95e46d8ba51c6a44586ea5..cf20cfafd70d30b49e6583df461c44ecb8b44ed6 100644 (file)
@@ -1,18 +1,38 @@
+#include <iomanip>
 #include <itkCommand.h>
 #include <itkImage.h>
 #include <itkImageFileReader.h>
 #include <itkImageFileWriter.h>
+#include <itkBinaryThresholdImageFilter.h>
 
 #include <fpa/Image/MoriRegionGrow.h>
 
 // -------------------------------------------------------------------------
 static const unsigned int VDim = 3;
-typedef short                          TPixel;
-typedef itk::Image< TPixel, VDim >     TImage;
-typedef itk::ImageFileReader< TImage > TReader;
-typedef itk::ImageFileWriter< TImage > TWriter;
-typedef fpa::Image::MoriRegionGrow< TImage, TImage > TFilter;
-typedef itk::ImageFileWriter< TFilter::TAuxiliaryImage > TAuxWriter;
+typedef short                                             TPixel;
+typedef itk::Image< TPixel, VDim >                        TImage;
+typedef itk::ImageFileReader< TImage >                    TReader;
+typedef itk::ImageFileWriter< TImage >                    TWriter;
+typedef fpa::Image::MoriRegionGrow< TImage, TImage >      TFilter;
+typedef itk::BinaryThresholdImageFilter< TImage, TImage > TThreshold;
+
+// -------------------------------------------------------------------------
+class ShowProgressObject
+{
+public:
+  ShowProgressObject( itk::ProcessObject* o )
+    {
+      this->m_Process = o;
+    }
+  void ShowProgress( )
+    {
+      std::cerr
+        << "\rProgress " << std::fixed << std::setprecision( 2 )
+        << ( this->m_Process->GetProgress( ) * 100 )
+        << " %" << std::flush;
+    }
+  itk::ProcessObject::Pointer m_Process;
+};
 
 // -------------------------------------------------------------------------
 int main( int argc, char* argv[] )
@@ -45,16 +65,35 @@ int main( int argc, char* argv[] )
   TImage::IndexType seed;
   for( int i = 0; i < VDim; ++i )
     seed[ i ] = std::atoi( argv[ i + 7 ] );
-  filter->SetSeed( seed );
-  filter->SetInsideValue( 255 );
-  filter->SetOutsideValue( 0 );
+  filter->AddSeed( seed );
+
+  // to test ProgressReporter
+  ShowProgressObject progressWatch( filter );
+  typedef itk::SimpleMemberCommand< ShowProgressObject > CommandType;
+  CommandType::Pointer command = CommandType::New();
+  command->SetCallbackFunction( &progressWatch,
+                                &ShowProgressObject::ShowProgress );
+  filter->AddObserver( itk::ProgressEvent( ), command );
+  filter->Update( );
 
+  TThreshold::Pointer threshold = TThreshold::New( );
+  threshold->SetInput( filter->GetOutput( ) );
+  threshold->SetInsideValue( 255 );
+  threshold->SetOutsideValue( 0 );
+  threshold->SetLowerThreshold( 0 );
+  threshold->SetUpperThreshold( filter->GetOptimumThreshold( ) );
+  
   TWriter::Pointer writer = TWriter::New( );
-  writer->SetInput( filter->GetOutput( ) );
+  writer->SetInput( threshold->GetOutput( ) );
   writer->SetFileName( output_image_filename );
+
+  TWriter::Pointer aux_writer = TWriter::New( );
+  aux_writer->SetInput( filter->GetOutput( ) );
+  aux_writer->SetFileName( auxiliary_image_filename );
   try
   {
     writer->Update( );
+    aux_writer->Update( );
   }
   catch( std::exception& err )
   {
@@ -63,19 +102,20 @@ int main( int argc, char* argv[] )
 
   } // yrt
 
-  TAuxWriter::Pointer aux_writer = TAuxWriter::New( );
-  aux_writer->SetInput( filter->GetAuxiliaryImage( ) );
-  aux_writer->SetFileName( auxiliary_image_filename );
-  try
+  // Show data
+  TFilter::TCurve curve = filter->GetCurve( );
+  for( TFilter::TCurveData data: curve )
   {
-    aux_writer->Update( );
+    std::cout << data.XValue << " " << data.YValue << " " << data.Diff1 << std::endl;
   }
-  catch( std::exception& err )
-  {
-    std::cerr << "ERROR: " << err.what( ) << std::endl;
-    return( 1 );
-
-  } // yrt
+  std::cerr
+    << std::endl
+    << "# Opt: "
+    << curve[ filter->GetOptimumThreshold( ) ].XValue
+    << "("
+    << filter->GetOptimumThreshold( )
+    << ")"
+    << std::endl;
   return( 0 );
 }