#include #include #include #include #include #include #include #include #include // ------------------------------------------------------------------------- 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 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 < 3 ) { std::cerr << "Usage: " << argv[ 0 ] << " input_image output_image" << 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"; // 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 ); // Mori's algorithms typedef fpa::Image::MoriFilter< TImage, TImage > TFilter; TFilter::Pointer filter = TFilter::New( ); filter->SetInput( reader->GetOutput( ) ); filter->SetSeed( seed ); filter->SetThresholdRange( -200, 100, 1 ); filter->SetInsideValue( 1 ); filter->SetOutsideValue( 0 ); // 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 // Write auxiliary image typedef itk::ImageFileWriter< TFilter::TAuxImage > TAuxWriter; TAuxWriter::Pointer aux_writer = TAuxWriter::New( ); aux_writer->SetInput( filter->GetAuxiliaryImage( ) ); aux_writer->SetFileName( output_auxiliary_image_filename ); // Execute pipeline try { aux_writer->Update( ); } catch( std::exception& err ) { std::cerr << "Error caught: " << err.what( ) << std::endl; return( 1 ); } // yrt // Write result signal std::string output_signal_filename = output_image_filename + ".csv"; std::ofstream output_signal_str( output_signal_filename.c_str( ) ); TFilter::TCurve curve = filter->GetCurve( ); unsigned long max_count = 0; for( TFilter::TCurve::value_type d : curve ) { output_signal_str << d.first << " " << d.second << std::endl; if( max_count < d.second ) max_count = d.second; } // rof output_signal_str.close( ); // Write gnuplot script std::string output_gnuplot_filename = output_image_filename + ".gnuplot"; std::ofstream output_gnuplot_str( output_gnuplot_filename.c_str( ) ); unsigned int thr_pos = filter->GetOptimumThreshold( ); int thr = curve[ thr_pos ].first; output_gnuplot_str << "set term png" << std::endl << "set output \"" << output_image_filename << ".png\"" << std::endl << "set arrow 1 from " << thr << ",0 to " << thr << "," << max_count << std::endl << "show arrow 1" << std::endl << "plot \"" << output_signal_filename << "\" using 1:2 with linespoints title \"Evolution (" << thr << "," << thr_pos << ") " << "\"" << std::endl; output_gnuplot_str.close( ); return( 0 ); } // eof - $RCSfile$