3 #include <itkCommand.h>
5 #include <itkImageFileReader.h>
6 #include <itkImageFileWriter.h>
7 #include <itkBinaryThresholdImageFilter.h>
9 #include <fpa/Image/MoriRegionGrow.h>
11 // -------------------------------------------------------------------------
12 static const unsigned int VDim = 3;
14 typedef itk::Image< TPixel, VDim > TImage;
15 typedef itk::ImageFileReader< TImage > TReader;
16 typedef itk::ImageFileWriter< TImage > TWriter;
17 typedef fpa::Image::MoriRegionGrow< TImage, TImage > TFilter;
18 typedef itk::BinaryThresholdImageFilter< TImage, TImage > TThreshold;
20 // -------------------------------------------------------------------------
21 class ShowProgressObject
24 ShowProgressObject( itk::ProcessObject* o )
31 << "\rProgress " << std::fixed << std::setprecision( 2 )
32 << ( this->m_Process->GetProgress( ) * 100 )
33 << " %" << std::flush;
35 itk::ProcessObject::Pointer m_Process;
38 // -------------------------------------------------------------------------
39 int main( int argc, char* argv[] )
45 << "Usage: " << argv[ 0 ]
46 << " input_image output_image auxiliary_image lower upper delta";
47 for( unsigned int i = 0; i < VDim; ++i )
48 std::cerr << " s_" << i;
49 std::cerr << std::endl;
53 std::string input_image_filename = argv[ 1 ];
54 std::string output_image_filename = argv[ 2 ];
55 std::string auxiliary_image_filename = argv[ 3 ];
56 TPixel lower = std::atof( argv[ 4 ] );
57 TPixel upper = std::atof( argv[ 5 ] );
58 TPixel delta = std::atof( argv[ 6 ] );
60 TReader::Pointer reader = TReader::New( );
61 reader->SetFileName( input_image_filename );
66 catch( std::exception& err )
68 std::cerr << "ERROR: " << err.what( ) << std::endl;
73 TFilter::Pointer filter = TFilter::New( );
74 filter->SetInput( reader->GetOutput( ) );
75 filter->SetThresholdRange( lower, upper, delta );
76 TImage::PointType pnt;
77 for( int i = 0; i < VDim; ++i )
78 pnt[ i ] = std::atof( argv[ i + 7 ] );
80 TImage::IndexType seed;
81 if( !( reader->GetOutput( )->TransformPhysicalPointToIndex( pnt, seed ) ) )
83 std::cerr << "ERROR: seed outside image." << std::endl;
87 filter->AddSeed( seed );
89 // to test ProgressReporter
91 ShowProgressObject progressWatch( filter );
92 typedef itk::SimpleMemberCommand< ShowProgressObject > CommandType;
93 CommandType::Pointer command = CommandType::New();
94 command->SetCallbackFunction( &progressWatch,
95 &ShowProgressObject::ShowProgress );
96 filter->AddObserver( itk::ProgressEvent( ), command );
98 std::chrono::time_point< std::chrono::system_clock > start, end;
99 start = std::chrono::system_clock::now( );
101 end = std::chrono::system_clock::now( );
102 std::chrono::duration< double > elapsed_seconds = end - start;
104 TThreshold::Pointer threshold = TThreshold::New( );
105 threshold->SetInput( filter->GetOutput( ) );
106 threshold->SetInsideValue( 255 );
107 threshold->SetOutsideValue( 0 );
108 threshold->SetLowerThreshold( 0 );
109 threshold->SetUpperThreshold( filter->GetOptimumThreshold( ) );
111 TWriter::Pointer writer = TWriter::New( );
112 writer->SetInput( threshold->GetOutput( ) );
113 writer->SetFileName( output_image_filename );
115 TWriter::Pointer aux_writer = TWriter::New( );
116 aux_writer->SetInput( filter->GetOutput( ) );
117 aux_writer->SetFileName( auxiliary_image_filename );
121 aux_writer->Update( );
123 catch( std::exception& err )
125 std::cerr << "ERROR: " << err.what( ) << std::endl;
131 TFilter::TCurve curve = filter->GetCurve( );
132 for( TFilter::TCurveData data: curve )
134 std::cout << data.XValue << " " << data.YValue << " " << data.Diff1 << std::endl;
139 << curve[ filter->GetOptimumThreshold( ) ].XValue
141 << filter->GetOptimumThreshold( )
144 std::cout << "Time: " << elapsed_seconds.count( ) << "s" << std::endl;