]> Creatis software - FrontAlgorithms.git/blob - tests/image/MoriSegmentation.cxx
7a84a5e467cab0dec20ec7701a2e9aee968c69b4
[FrontAlgorithms.git] / tests / image / MoriSegmentation.cxx
1 #include "BaseFunctions.h"
2 #include <chrono>
3 #include <itkImage.h>
4 #include <fpa/Image/Mori.h>
5
6 // -------------------------------------------------------------------------
7 const unsigned int Dim = 3;
8 typedef short          TPixel;
9 typedef unsigned short TLabel;
10
11 typedef itk::Image< TPixel, Dim > TInputImage;
12 typedef itk::Image< TLabel, Dim > TLabelImage;
13 typedef fpa::Image::Mori< TInputImage, TLabelImage > TFilter;
14
15 // -------------------------------------------------------------------------
16 int main( int argc, char* argv[] )
17 {
18   // Get arguments
19   if( argc < 9 + Dim )
20   {
21     std::cerr
22       << "Usage: " << argv[ 0 ]
23       << " input_image output_image output_marks output_signal"
24       << " init_threshold end_threshold delta [index/point] seed"
25       << std::endl;
26     return( 1 );
27
28   } // fi
29   std::string input_image_filename = argv[ 1 ];
30   std::string output_image_filename = argv[ 2 ];
31   std::string output_marks_filename = argv[ 3 ];
32   std::string output_signal_filename = argv[ 4 ];
33   TPixel init_threshold = std::atoi( argv[ 5 ] );
34   TPixel end_threshold = std::atoi( argv[ 6 ] );
35   TPixel delta = std::atoi( argv[ 7 ] );
36   std::string seed_type = argv[ 8 ];
37
38   TInputImage::IndexType iseed;
39   TInputImage::PointType pseed;
40   for( unsigned int i = 0; i < Dim; ++i )
41   {
42     if( seed_type == "index" )
43       iseed[ i ] = std::atoi( argv[ 9 + i ] );
44     else
45       pseed[ i ] = std::atof( argv[ 9 + i ] );
46
47   } // rof
48
49   // Create image
50   TInputImage::Pointer input_image;
51   std::string err0 =
52     fpa::tests::image::Read( input_image, input_image_filename );
53   if( err0 != "" ) std::cerr << err0 << std::endl;
54
55   // Prepare filter
56   TFilter::Pointer filter = TFilter::New( );
57   filter->SetInput( input_image );
58   if( seed_type == "index" )
59     filter->SetSeed( iseed );
60   else
61     filter->SetSeed( pseed );
62   filter->SetThresholds( init_threshold, end_threshold, delta );
63   filter->SetInsideValue( 255 );
64   filter->SetOutsideValue( 0 );
65   filter->SetSignalLag( 20 );
66   filter->SetSignalThreshold( 500 );
67   filter->SetSignalInfluence( 0.5 );
68   filter->SetMinimumThreshold( -850 );
69
70   // Execute filter
71   std::chrono::time_point< std::chrono::high_resolution_clock > tstart, tend;
72   tstart = std::chrono::high_resolution_clock::now( );
73   filter->Update( );
74   tend = std::chrono::high_resolution_clock::now( );
75   std::chrono::duration< double > telapsed = tend - tstart;
76
77   // Save results
78   std::string err1 =
79     fpa::tests::image::Write( filter->GetThresholdedOutput( ), output_image_filename );
80   std::string err2 =
81     fpa::tests::image::Write( filter->GetMarks( ), output_marks_filename );
82   if( err1 != "" ) std::cerr << err1 << std::endl;
83   if( err2 != "" ) std::cerr << err2 << std::endl;
84
85   std::ofstream osignal( output_signal_filename.c_str( ) );
86   const TFilter::TSignal& signal = filter->GetSignal( );
87   for( unsigned long i = 0; i < signal.size( ); ++i )
88     osignal << signal[ i ].first << " " << signal[ i ].second << std::endl;
89   osignal.close( );
90
91   std::cout
92     << "------------------------------------------------------" << std::endl
93     << "Elapsed time: " << telapsed.count( ) << " s" << std::endl
94     << "Optimum threshold: "
95     << filter->GetOptimumThreshold( ) << std::endl
96     << "Number of evaluated thresholds: "
97     << filter->GetNumberOfEvaluatedThresholds( ) << std::endl
98     << "------------------------------------------------------"
99     << std::endl;
100
101   return( 0 );
102 }
103
104 // eof - $RCSfile$