+#include <climits>
+#include <cstdlib>
+#include <fstream>
+#include <iostream>
+#include <string>
+
+#include <itkImage.h>
+#include <itkImageFileReader.h>
+#include <itkImageFileWriter.h>
+
+#include <fpa/Image/RegionGrow.h>
+#include <fpa/Image/Functors/RegionGrow/BinaryThreshold.h>
+
+// -------------------------------------------------------------------------
+static const unsigned int Dim = 3;
+typedef short TPixel;
+typedef itk::Image< TPixel, Dim > TImage;
+
+// -------------------------------------------------------------------------
+std::string GetFullPath( const std::string& filename )
+{
+ /* On windows:
+ #include <windows.h>
+ TCHAR full_path[MAX_PATH];
+ GetFullPathName(_T("foo.dat"), MAX_PATH, full_path, NULL);
+ */
+
+ char* buffer = realpath( filename.c_str( ), NULL );
+ std::string path = buffer;
+ std::free( buffer );
+ return( path );
+}
+
+// -------------------------------------------------------------------------
+std::string GetFullPathToDirectory( const std::string& filename )
+{
+ std::string path = GetFullPath( filename );
+ std::size_t found = path.find_last_of( "/\\" );
+ return( path.substr( 0, found + 1 ) );
+}
+
+// -------------------------------------------------------------------------
+int main( int argc, char* argv[] )
+{
+ // Command configuration
+ if( argc < 5 )
+ {
+ std::cerr
+ << "Usage: " << argv[ 0 ] << " input_image output_image lower upper"
+ << std::endl;
+ return( 1 );
+
+ } // fi
+ std::string input_image_filename = GetFullPath( argv[ 1 ] );
+ std::string output_image_filename = argv[ 2 ];
+ std::string output_auxiliary_image_filename = output_image_filename + "_aux.mhd";
+ TPixel lower = std::atoi( argv[ 3 ] );
+ TPixel upper = std::atoi( argv[ 4 ] );
+
+ // Try to guess initial seed
+ std::string seed_filename = GetFullPathToDirectory( argv[ 1 ] ) + "seed.txt";
+ std::ifstream seed_str( seed_filename.c_str( ) );
+ if( !seed_str )
+ {
+ std::cerr
+ << "No \"seed.txt\" file in the same input image directory."
+ << std::endl;
+ return( 1 );
+
+ } // fi
+ TImage::IndexType seed;
+ seed_str >> seed[ 0 ] >> seed[ 1 ] >> seed[ 2 ];
+ seed_str.close( );
+
+ // Read image
+ typedef itk::ImageFileReader< TImage > TReader;
+ TReader::Pointer reader = TReader::New( );
+ reader->SetFileName( input_image_filename );
+
+ // Growing predicate
+ typedef fpa::Image::Functors::RegionGrow::BinaryThreshold< TImage, TImage::PixelType > TPredicate;
+ TPredicate::Pointer predicate = TPredicate::New( );
+ predicate->SetLower( lower );
+ predicate->SetUpper( upper );
+
+ // RegionGrow algorithm
+ typedef fpa::Image::RegionGrow< TImage, TImage > TFilter;
+ TFilter::Pointer filter = TFilter::New( );
+ filter->SetInput( reader->GetOutput( ) );
+ filter->AddSeed( seed, 1 );
+ filter->SetInsideValue( 1 );
+ filter->SetOutsideValue( 0 );
+ filter->SetGrowFunction( predicate );
+
+ // Write results
+ typedef itk::ImageFileWriter< TImage > TWriter;
+ TWriter::Pointer writer = TWriter::New( );
+ writer->SetInput( filter->GetOutput( ) );
+ writer->SetFileName( output_image_filename );
+
+ // Execute pipeline
+ try
+ {
+ writer->Update( );
+ }
+ catch( std::exception& err )
+ {
+ std::cerr << "Error caught: " << err.what( ) << std::endl;
+ return( 1 );
+
+ } // yrt
+
+ return( 0 );
+}
+
+// eof - $RCSfile$