8 #include <itkImageFileReader.h>
9 #include <itkImageFileWriter.h>
11 #include <fpa/Image/RegionGrow.h>
12 #include <fpa/Image/Functors/RegionGrow/BinaryThreshold.h>
14 // -------------------------------------------------------------------------
15 static const unsigned int Dim = 3;
17 typedef itk::Image< TPixel, Dim > TImage;
19 // -------------------------------------------------------------------------
20 std::string GetFullPath( const std::string& filename )
24 TCHAR full_path[MAX_PATH];
25 GetFullPathName(_T("foo.dat"), MAX_PATH, full_path, NULL);
28 char* buffer = realpath( filename.c_str( ), NULL );
29 std::string path = buffer;
34 // -------------------------------------------------------------------------
35 std::string GetFullPathToDirectory( const std::string& filename )
37 std::string path = GetFullPath( filename );
38 std::size_t found = path.find_last_of( "/\\" );
39 return( path.substr( 0, found + 1 ) );
42 // -------------------------------------------------------------------------
43 int main( int argc, char* argv[] )
45 // Command configuration
49 << "Usage: " << argv[ 0 ] << " input_image output_image lower upper"
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 ] );
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( ) );
66 << "No \"seed.txt\" file in the same input image directory."
71 TImage::IndexType seed;
72 seed_str >> seed[ 0 ] >> seed[ 1 ] >> seed[ 2 ];
76 typedef itk::ImageFileReader< TImage > TReader;
77 TReader::Pointer reader = TReader::New( );
78 reader->SetFileName( input_image_filename );
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 );
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 );
96 typedef itk::ImageFileWriter< TImage > TWriter;
97 TWriter::Pointer writer = TWriter::New( );
98 writer->SetInput( filter->GetOutput( ) );
99 writer->SetFileName( output_image_filename );
106 catch( std::exception& err )
108 std::cerr << "Error caught: " << err.what( ) << std::endl;