]> Creatis software - FrontAlgorithms.git/blob - appli/CTBronchi/Process.cxx
...
[FrontAlgorithms.git] / appli / CTBronchi / Process.cxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #include <itkImage.h>
7 #include <itkAbsoluteValueDifferenceImageFilter.h>
8 #include <itkBinaryThresholdImageFilter.h>
9 #include <CTBronchi/Functions.h>
10 #include <CTBronchi/Mori.h>
11 #include <CTBronchi/MoriLabelling.h>
12 #include <CTBronchi/RandomWalker.h>
13
14 // -------------------------------------------------------------------------
15 const unsigned int Dim = 3;
16 typedef short         TInputPixel;
17 typedef unsigned char TLabelPixel;
18 typedef itk::Image< TInputPixel, Dim > TImage;
19 typedef itk::Image< TLabelPixel, Dim > TLabelImage;
20
21 // -------------------------------------------------------------------------
22 int main( int argc, char* argv[] )
23 {
24   std::map< std::string, std::string > args;
25   try
26   {
27     if( CTBronchi::ParseArgs( args, argc, argv ) )
28     {
29       // Parse seed
30       TImage::PointType seed;
31       char* str = new char[ args[ "seed" ].size( ) + 1 ];
32       strcpy( str, args[ "seed" ].c_str( ) );
33       seed[ 0 ] = std::atof( strtok( str, ";" ) );
34       seed[ 1 ] = std::atof( strtok( NULL, ";" ) );
35       seed[ 2 ] = std::atof( strtok( NULL, ";" ) );
36
37       // Read input image
38       TImage::Pointer input_image;
39       CTBronchi::ReadImage( input_image, args[ "in" ] );
40
41       // Mori segmentation
42       TLabelImage::Pointer mori;
43       TInputPixel opt_thr = CTBronchi::Mori( mori, input_image, seed, args );
44
45       // Label image
46       TLabelImage::Pointer labels;
47       CTBronchi::Label( labels, input_image, mori, opt_thr, args );
48
49       // Final labels
50       TLabelImage::Pointer final_labels;
51       CTBronchi::RandomWalker( final_labels, input_image, labels, args );
52
53       // Extract label 1
54       typedef itk::BinaryThresholdImageFilter< TLabelImage, TLabelImage > TBinThr;
55       TBinThr::Pointer bin_thr = TBinThr::New( );
56       bin_thr->SetInput( final_labels );
57       bin_thr->SetLowerThreshold( 1 );
58       bin_thr->SetUpperThreshold( 1 );
59       bin_thr->SetInsideValue( 1 );
60       bin_thr->SetOutsideValue( 0 );
61       double t = CTBronchi::MeasureTime( bin_thr );
62       std::cout << "Label extracted in " << t << " s" << std::endl;
63       TLabelImage::Pointer segmentation = bin_thr->GetOutput( );
64
65       // Save
66       CTBronchi::WriteImage( segmentation, args[ "out" ] );
67
68       // Save difference
69       std::map< std::string, std::string >::const_iterator i =
70         args.find( "out_diff" );
71       if( i != args.end( ) )
72       {
73         typedef itk::AbsoluteValueDifferenceImageFilter< TLabelImage, TLabelImage, TLabelImage > TDiff;
74         TDiff::Pointer diff = TDiff::New( );
75         diff->SetInput1( mori );
76         diff->SetInput2( segmentation );
77         t = CTBronchi::MeasureTime( diff );
78         std::cout << "Difference computed in " << t << " s" << std::endl;
79         TLabelImage::Pointer difference = diff->GetOutput( );
80         CTBronchi::WriteImage( difference, i->second );
81
82       } // fi
83     }
84     else
85       return( 1 );
86   }
87   catch( std::exception& err )
88   {
89     std::cerr
90       << "===============================" << std::endl
91       << "Error caught: " << std::endl
92       << err.what( )
93       << "===============================" << std::endl
94       << std::endl;
95     return( 1 );
96
97   } // yrt
98   return( 0 );
99 }
100
101 // eof - $RCSfile$