]> Creatis software - FrontAlgorithms.git/blob - tests/image/MoriSegmentation.cxx
f2dd92a0e8147e22436acf13e8fbcc78d93d68d3
[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
69   // Execute filter
70   std::chrono::time_point< std::chrono::high_resolution_clock > tstart, tend;
71   tstart = std::chrono::high_resolution_clock::now( );
72   filter->Update( );
73   tend = std::chrono::high_resolution_clock::now( );
74   std::chrono::duration< double > telapsed = tend - tstart;
75
76   // Save results
77   std::string err1 =
78     fpa::tests::image::Write( filter->GetThresholdedOutput( ), output_image_filename );
79   std::string err2 =
80     fpa::tests::image::Write( filter->GetMarks( ), output_marks_filename );
81   if( err1 != "" ) std::cerr << err1 << std::endl;
82   if( err2 != "" ) std::cerr << err2 << std::endl;
83
84   std::ofstream osignal( output_signal_filename.c_str( ) );
85   const TFilter::TSignal& signal = filter->GetSignal( );
86   for( unsigned long i = 0; i < signal.size( ); ++i )
87     osignal << signal[ i ].first << " " << signal[ i ].second << std::endl;
88   osignal.close( );
89
90   std::cout
91     << "------------------------------------------------------" << std::endl
92     << "Elapsed time: " << telapsed.count( ) << " s" << std::endl
93     << "Optimum threshold: "
94     << filter->GetOptimumThreshold( ) << std::endl
95     << "Number of evaluated thresholds: "
96     << filter->GetNumberOfEvaluatedThresholds( ) << std::endl
97     << "------------------------------------------------------"
98     << std::endl;
99
100   return( 0 );
101 }
102
103 // eof - $RCSfile$