]> Creatis software - FrontAlgorithms.git/blob - examples/RegionGrow_Mori.cxx
...
[FrontAlgorithms.git] / examples / RegionGrow_Mori.cxx
1 #include <iomanip>
2 #include <itkCommand.h>
3 #include <itkImage.h>
4 #include <itkImageFileReader.h>
5 #include <itkImageFileWriter.h>
6 #include <itkBinaryThresholdImageFilter.h>
7
8 #include <fpa/Image/MoriRegionGrow.h>
9
10 // -------------------------------------------------------------------------
11 static const unsigned int VDim = 3;
12 typedef short                                             TPixel;
13 typedef itk::Image< TPixel, VDim >                        TImage;
14 typedef itk::ImageFileReader< TImage >                    TReader;
15 typedef itk::ImageFileWriter< TImage >                    TWriter;
16 typedef fpa::Image::MoriRegionGrow< TImage, TImage >      TFilter;
17 typedef itk::BinaryThresholdImageFilter< TImage, TImage > TThreshold;
18
19 // -------------------------------------------------------------------------
20 class ShowProgressObject
21 {
22 public:
23   ShowProgressObject( itk::ProcessObject* o )
24     {
25       this->m_Process = o;
26     }
27   void ShowProgress( )
28     {
29       std::cerr
30         << "\rProgress " << std::fixed << std::setprecision( 2 )
31         << ( this->m_Process->GetProgress( ) * 100 )
32         << " %" << std::flush;
33     }
34   itk::ProcessObject::Pointer m_Process;
35 };
36
37 // -------------------------------------------------------------------------
38 int main( int argc, char* argv[] )
39 {
40   // Get arguments
41   if( argc < 7 + VDim )
42   {
43     std::cerr
44       << "Usage: " << argv[ 0 ]
45       << " input_image output_image auxiliary_image lower upper delta";
46     for( unsigned int i = 0; i < VDim; ++i )
47       std::cerr << " s_" << i;
48     std::cerr << std::endl;
49     return( 1 );
50
51   } // fi
52   std::string input_image_filename = argv[ 1 ];
53   std::string output_image_filename = argv[ 2 ];
54   std::string auxiliary_image_filename = argv[ 3 ];
55   TPixel lower = std::atof( argv[ 4 ] );
56   TPixel upper = std::atof( argv[ 5 ] );
57   TPixel delta = std::atof( argv[ 6 ] );
58
59   TReader::Pointer reader = TReader::New( );
60   reader->SetFileName( input_image_filename );
61
62   TFilter::Pointer filter = TFilter::New( );
63   filter->SetInput( reader->GetOutput( ) );
64   filter->SetThresholdRange( lower, upper, delta );
65   TImage::IndexType seed;
66   for( int i = 0; i < VDim; ++i )
67     seed[ i ] = std::atoi( argv[ i + 7 ] );
68   filter->AddSeed( seed );
69
70   // to test ProgressReporter
71   ShowProgressObject progressWatch( filter );
72   typedef itk::SimpleMemberCommand< ShowProgressObject > CommandType;
73   CommandType::Pointer command = CommandType::New();
74   command->SetCallbackFunction( &progressWatch,
75                                 &ShowProgressObject::ShowProgress );
76   filter->AddObserver( itk::ProgressEvent( ), command );
77   filter->Update( );
78
79   TThreshold::Pointer threshold = TThreshold::New( );
80   threshold->SetInput( filter->GetOutput( ) );
81   threshold->SetInsideValue( 255 );
82   threshold->SetOutsideValue( 0 );
83   threshold->SetLowerThreshold( 0 );
84   threshold->SetUpperThreshold( filter->GetOptimumThreshold( ) );
85   
86   TWriter::Pointer writer = TWriter::New( );
87   writer->SetInput( threshold->GetOutput( ) );
88   writer->SetFileName( output_image_filename );
89
90   TWriter::Pointer aux_writer = TWriter::New( );
91   aux_writer->SetInput( filter->GetOutput( ) );
92   aux_writer->SetFileName( auxiliary_image_filename );
93   try
94   {
95     writer->Update( );
96     aux_writer->Update( );
97   }
98   catch( std::exception& err )
99   {
100     std::cerr << "ERROR: " << err.what( ) << std::endl;
101     return( 1 );
102
103   } // yrt
104
105   // Show data
106   TFilter::TCurve curve = filter->GetCurve( );
107   for( TFilter::TCurveData data: curve )
108   {
109     std::cout << data.XValue << " " << data.YValue << " " << data.Diff1 << std::endl;
110   }
111   std::cerr
112     << std::endl
113     << "# Opt: "
114     << curve[ filter->GetOptimumThreshold( ) ].XValue
115     << "("
116     << filter->GetOptimumThreshold( )
117     << ")"
118     << std::endl;
119   return( 0 );
120 }
121
122 // eof - $RCSfile$