+#include <chrono>
+#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::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[] )
+{
+ // Get arguments
+ if( argc < 6 + VDim )
+ {
+ std::cerr
+ << "Usage: " << argv[ 0 ]
+ << " input_image output_image lower upper delta";
+ for( unsigned int i = 0; i < VDim; ++i )
+ std::cerr << " s_" << i;
+ std::cerr << std::endl;
+ return( 1 );
+
+ } // fi
+ std::string input_image_filename = argv[ 1 ];
+ std::string output_image_filename = argv[ 2 ];
+ TPixel lower = std::atof( argv[ 3 ] );
+ TPixel upper = std::atof( argv[ 4 ] );
+ TPixel delta = std::atof( argv[ 5 ] );
+
+ TReader::Pointer reader = TReader::New( );
+ reader->SetFileName( input_image_filename );
+ try
+ {
+ reader->Update( );
+ }
+ catch( std::exception& err )
+ {
+ std::cerr << "ERROR: " << err.what( ) << std::endl;
+ return( 1 );
+
+ } // yrt
+
+ TFilter::Pointer filter = TFilter::New( );
+ filter->SetInput( reader->GetOutput( ) );
+ filter->SetThresholdRange( lower, upper, delta );
+ TImage::PointType pnt;
+ for( int i = 0; i < VDim; ++i )
+ pnt[ i ] = std::atof( argv[ i + 6 ] );
+
+ TImage::IndexType seed;
+ if( !( reader->GetOutput( )->TransformPhysicalPointToIndex( pnt, seed ) ) )
+ {
+ std::cerr << "ERROR: seed outside image." << std::endl;
+ return( 1 );
+
+ } // fi
+ filter->AddSeed( seed );
+
+ // to test ProgressReporter
+ /* TODO
+ ShowProgressObject progressWatch( filter );
+ typedef itk::SimpleMemberCommand< ShowProgressObject > CommandType;
+ CommandType::Pointer command = CommandType::New();
+ command->SetCallbackFunction( &progressWatch,
+ &ShowProgressObject::ShowProgress );
+ filter->AddObserver( itk::ProgressEvent( ), command );
+ */
+ std::chrono::time_point< std::chrono::system_clock > start, end;
+ start = std::chrono::system_clock::now( );
+ filter->Update( );
+ end = std::chrono::system_clock::now( );
+ std::chrono::duration< double > elapsed_seconds = end - start;
+
+ 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( threshold->GetOutput( ) );
+ writer->SetFileName( output_image_filename );
+ try
+ {
+ writer->Update( );
+ }
+ catch( std::exception& err )
+ {
+ std::cerr << "ERROR: " << err.what( ) << std::endl;
+ return( 1 );
+
+ } // yrt
+
+ // Show data
+ TFilter::TCurve curve = filter->GetCurve( );
+ for( TFilter::TCurveData data: curve )
+ {
+ std::cout << data.XValue << " " << data.YValue << " " << data.Diff1 << std::endl;
+ }
+ std::cout
+ << std::endl
+ << "# Opt: "
+ << curve[ filter->GetOptimumThreshold( ) ].XValue
+ << "("
+ << filter->GetOptimumThreshold( )
+ << ")"
+ << std::endl;
+ std::cout << "Time: " << elapsed_seconds.count( ) << "s" << std::endl;
+ return( 0 );
+}
+
+// eof - $RCSfile$