]> Creatis software - FrontAlgorithms.git/blob - examples/BronchiiInitialSegmentationWithMori.cxx
ed0c294132e264d2a11558f45616a870ac89bdd9
[FrontAlgorithms.git] / examples / BronchiiInitialSegmentationWithMori.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/MoriFilter.h>
12
13 // -------------------------------------------------------------------------
14 static const unsigned int Dim = 3;
15 typedef short TPixel;
16 typedef itk::Image< TPixel, Dim > TImage;
17
18 // -------------------------------------------------------------------------
19 std::string GetFullPath( const std::string& filename )
20 {
21   /* On windows:
22      #include <windows.h>
23      TCHAR full_path[MAX_PATH];
24      GetFullPathName(_T("foo.dat"), MAX_PATH, full_path, NULL);
25   */
26
27   char* buffer = realpath( filename.c_str( ), NULL );
28   std::string path = buffer;
29   std::free( buffer );
30   return( path );
31 }
32
33 // -------------------------------------------------------------------------
34 std::string GetFullPathToDirectory( const std::string& filename )
35 {
36   std::string path = GetFullPath( filename );
37   std::size_t found = path.find_last_of( "/\\" );
38   return( path.substr( 0, found + 1 ) );
39 }
40
41 // -------------------------------------------------------------------------
42 int main( int argc, char* argv[] )
43 {
44   // Command configuration
45   if( argc < 3 )
46   {
47     std::cerr
48       << "Usage: " << argv[ 0 ] << " input_image output_image"
49       << std::endl;
50     return( 1 );
51
52   } // fi
53   std::string input_image_filename = GetFullPath( argv[ 1 ] );
54   std::string output_image_filename = argv[ 2 ];
55
56   // Try to guess initial seed
57   std::string seed_filename = GetFullPathToDirectory( argv[ 1 ] ) + "seed.txt";
58   std::ifstream seed_str( seed_filename.c_str( ) );
59   if( !seed_str )
60   {
61     std::cerr
62       << "No \"seed.txt\" file in the same input image directory."
63       << std::endl;
64     return( 1 );
65
66   } // fi
67   TImage::IndexType seed;
68   seed_str >> seed[ 0 ] >> seed[ 1 ] >> seed[ 2 ];
69
70   // Read image
71   typedef itk::ImageFileReader< TImage > TReader;
72   TReader::Pointer reader = TReader::New( );
73   reader->SetFileName( input_image_filename );
74
75   // Mori's algorithms
76   typedef fpa::Image::MoriFilter< TImage, TImage > TFilter;
77   TFilter::Pointer filter = TFilter::New( );
78   filter->SetInput( reader->GetOutput( ) );
79   filter->SetSeed( seed );
80   filter->SetThresholdRange( -1024, 0 );
81   filter->SetInsideValue( 1 );
82   filter->SetOutsideValue( 0 );
83
84   // Write results
85   typedef itk::ImageFileWriter< TImage > TWriter;
86   TWriter::Pointer writer = TWriter::New( );
87   writer->SetInput( filter->GetOutput( ) );
88   writer->SetFileName( output_image_filename );
89
90   // Execute pipeline
91   try
92   {
93     writer->Update( );
94   }
95   catch( std::exception& err )
96   {
97     std::cerr << "Error caught: " << err.what( ) << std::endl;
98     return( 1 );
99
100   } // yrt
101   return( 0 );
102 }
103
104 // eof - $RCSfile$