]> Creatis software - FrontAlgorithms.git/blob - examples/RegionGrow_Mori.cxx
...
[FrontAlgorithms.git] / examples / RegionGrow_Mori.cxx
1 #include <chrono>
2 #include <iomanip>
3 #include <itkCommand.h>
4 #include <itkImage.h>
5 #include <itkImageFileReader.h>
6 #include <itkImageFileWriter.h>
7 #include <itkBinaryThresholdImageFilter.h>
8
9 #include <fpa/Image/MoriRegionGrow.h>
10
11 // -------------------------------------------------------------------------
12 static const unsigned int VDim = 3;
13 typedef short                                             TPixel;
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;
19
20 // -------------------------------------------------------------------------
21 class ShowProgressObject
22 {
23 public:
24   ShowProgressObject( itk::ProcessObject* o )
25     {
26       this->m_Process = o;
27     }
28   void ShowProgress( )
29     {
30       std::cerr
31         << "\rProgress " << std::fixed << std::setprecision( 2 )
32         << ( this->m_Process->GetProgress( ) * 100 )
33         << " %" << std::flush;
34     }
35   itk::ProcessObject::Pointer m_Process;
36 };
37
38 // -------------------------------------------------------------------------
39 int main( int argc, char* argv[] )
40 {
41   // Get arguments
42   if( argc < 7 + VDim )
43   {
44     std::cerr
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;
50     return( 1 );
51
52   } // fi
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 ] );
59
60   TReader::Pointer reader = TReader::New( );
61   reader->SetFileName( input_image_filename );
62   try
63   {
64     reader->Update( );
65   }
66   catch( std::exception& err )
67   {
68     std::cerr << "ERROR: " << err.what( ) << std::endl;
69     return( 1 );
70
71   } // yrt
72
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 ] );
79
80   TImage::IndexType seed;
81   if( !( reader->GetOutput( )->TransformPhysicalPointToIndex( pnt, seed ) ) )
82   {
83     std::cerr << "ERROR: seed outside image." << std::endl;
84     return( 1 );
85
86   } // fi
87   filter->AddSeed( seed );
88
89   // to test ProgressReporter
90   /* TODO
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 );
97   */
98   std::chrono::time_point< std::chrono::system_clock > start, end;
99   start = std::chrono::system_clock::now( );
100   filter->Update( );
101   end = std::chrono::system_clock::now( );
102   std::chrono::duration< double > elapsed_seconds = end - start;
103
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( ) );
110   
111   TWriter::Pointer writer = TWriter::New( );
112   writer->SetInput( threshold->GetOutput( ) );
113   writer->SetFileName( output_image_filename );
114
115   TWriter::Pointer aux_writer = TWriter::New( );
116   aux_writer->SetInput( filter->GetOutput( ) );
117   aux_writer->SetFileName( auxiliary_image_filename );
118   try
119   {
120     writer->Update( );
121     aux_writer->Update( );
122   }
123   catch( std::exception& err )
124   {
125     std::cerr << "ERROR: " << err.what( ) << std::endl;
126     return( 1 );
127
128   } // yrt
129
130   // Show data
131   TFilter::TCurve curve = filter->GetCurve( );
132   for( TFilter::TCurveData data: curve )
133   {
134     std::cout << data.XValue << " " << data.YValue << " " << data.Diff1 << std::endl;
135   }
136   std::cout
137     << std::endl
138     << "# Opt: "
139     << curve[ filter->GetOptimumThreshold( ) ].XValue
140     << "("
141     << filter->GetOptimumThreshold( )
142      << ")"
143     << std::endl;
144   std::cout << "Time: " << elapsed_seconds.count( ) << "s" << std::endl;
145   return( 0 );
146 }
147
148 // eof - $RCSfile$