]> Creatis software - FrontAlgorithms.git/blob - examples/BronchiiInitialSegmentationWithBinaryThresholdRegionGrow.cxx
...
[FrontAlgorithms.git] / examples / BronchiiInitialSegmentationWithBinaryThresholdRegionGrow.cxx
1 #include <climits>
2 #include <cstdlib>
3 #include <fstream>
4 #include <iostream>
5 #include <string>
6
7 #include <itkImage.h>
8 #include <itkImageFileReader.h>
9 #include <itkImageFileWriter.h>
10
11 #include <fpa/Image/RegionGrow.h>
12 #include <fpa/Image/Functors/RegionGrow/BinaryThreshold.h>
13
14 // -------------------------------------------------------------------------
15 static const unsigned int Dim = 3;
16 typedef short TPixel;
17 typedef itk::Image< TPixel, Dim > TImage;
18
19 // -------------------------------------------------------------------------
20 std::string GetFullPath( const std::string& filename )
21 {
22   /* On windows:
23      #include <windows.h>
24      TCHAR full_path[MAX_PATH];
25      GetFullPathName(_T("foo.dat"), MAX_PATH, full_path, NULL);
26   */
27
28   char* buffer = realpath( filename.c_str( ), NULL );
29   std::string path = buffer;
30   std::free( buffer );
31   return( path );
32 }
33
34 // -------------------------------------------------------------------------
35 std::string GetFullPathToDirectory( const std::string& filename )
36 {
37   std::string path = GetFullPath( filename );
38   std::size_t found = path.find_last_of( "/\\" );
39   return( path.substr( 0, found + 1 ) );
40 }
41
42 // -------------------------------------------------------------------------
43 int main( int argc, char* argv[] )
44 {
45   // Command configuration
46   if( argc < 5 )
47   {
48     std::cerr
49       << "Usage: " << argv[ 0 ] << " input_image output_image lower upper"
50       << std::endl;
51     return( 1 );
52
53   } // fi
54   std::string input_image_filename = GetFullPath( argv[ 1 ] );
55   std::string output_image_filename = argv[ 2 ];
56   std::string output_auxiliary_image_filename = output_image_filename + "_aux.mhd";
57   TPixel lower = std::atoi( argv[ 3 ] );
58   TPixel upper = std::atoi( argv[ 4 ] );
59
60   // Try to guess initial seed
61   std::string seed_filename = GetFullPathToDirectory( argv[ 1 ] ) + "seed.txt";
62   std::ifstream seed_str( seed_filename.c_str( ) );
63   if( !seed_str )
64   {
65     std::cerr
66       << "No \"seed.txt\" file in the same input image directory."
67       << std::endl;
68     return( 1 );
69
70   } // fi
71   TImage::IndexType seed;
72   seed_str >> seed[ 0 ] >> seed[ 1 ] >> seed[ 2 ];
73   seed_str.close( );
74
75   // Read image
76   typedef itk::ImageFileReader< TImage > TReader;
77   TReader::Pointer reader = TReader::New( );
78   reader->SetFileName( input_image_filename );
79
80   // Growing predicate
81   typedef fpa::Image::Functors::RegionGrow::BinaryThreshold< TImage, TImage::PixelType > TPredicate;
82   TPredicate::Pointer predicate = TPredicate::New( );
83   predicate->SetLower( lower );
84   predicate->SetUpper( upper );
85
86   // RegionGrow algorithm
87   typedef fpa::Image::RegionGrow< TImage, TImage > TFilter;
88   TFilter::Pointer filter = TFilter::New( );
89   filter->SetInput( reader->GetOutput( ) );
90   filter->AddSeed( seed, 1 );
91   filter->SetInsideValue( 1 );
92   filter->SetOutsideValue( 0 );
93   filter->SetGrowFunction( predicate );
94
95   // Write results
96   typedef itk::ImageFileWriter< TImage > TWriter;
97   TWriter::Pointer writer = TWriter::New( );
98   writer->SetInput( filter->GetOutput( ) );
99   writer->SetFileName( output_image_filename );
100
101   // Execute pipeline
102   try
103   {
104     writer->Update( );
105   }
106   catch( std::exception& err )
107   {
108     std::cerr << "Error caught: " << err.what( ) << std::endl;
109     return( 1 );
110
111   } // yrt
112
113   return( 0 );
114 }
115
116 // eof - $RCSfile$