// @email florez-l@javeriana.edu.co
// =========================================================================
-#include <chrono>
-#include <map>
+#include <string>
+#include <tclap/CmdLine.h>
#include <itkImage.h>
-#include <itkImageFileReader.h>
-#include <itkImageFileWriter.h>
-#include <fpa/Filters/Image/Mori.h>
+#include "MoriLabelling.h"
+#include "Functions.h"
// -------------------------------------------------------------------------
const unsigned int Dim = 3;
-typedef short TPixel;
-typedef unsigned char TLabel;
-typedef itk::Image< TPixel, Dim > TImage;
-typedef itk::Image< TLabel, Dim > TLabels;
+typedef short TPixel;
+typedef unsigned char TLabel;
+typedef itk::NumericTraits< TPixel >::RealType TScalar;
+typedef itk::Image< TPixel, Dim > TImage;
+typedef itk::Image< TLabel, Dim > TLabels;
+typedef itk::Image< TScalar, Dim > TScalarImage;
// -------------------------------------------------------------------------
-double MeasureTime( itk::ProcessObject* f )
-{
- std::chrono::time_point< std::chrono::high_resolution_clock > s, e;
- std::chrono::duration< double > t;
- s = std::chrono::high_resolution_clock::now( );
- f->Update( );
- e = std::chrono::high_resolution_clock::now( );
- t = e - s;
- return( t.count( ) );
-}
-
-// -------------------------------------------------------------------------
-template< class _TImagePtr >
-void ReadImage( _TImagePtr& image, const std::string& fname )
-{
- typedef typename _TImagePtr::ObjectType _TImage;
- typedef itk::ImageFileReader< _TImage > _TReader;
- typename _TReader::Pointer reader = _TReader::New( );
- reader->SetFileName( fname );
- double t = MeasureTime( reader );
- std::cout << "Read " << fname << " in " << t << " s" << std::endl;
- image = reader->GetOutput( );
- image->DisconnectPipeline( );
-}
-
-// -------------------------------------------------------------------------
-template< class _TImage >
-void WriteImage( const _TImage*image, const std::string& fname )
-{
- typedef itk::ImageFileWriter< _TImage > _TWriter;
- typename _TWriter::Pointer writer = _TWriter::New( );
- writer->SetFileName( fname );
- writer->SetInput( image );
- double t = MeasureTime( writer );
- std::cout << "Wrote " << fname << " in " << t << " s" << std::endl;
-}
-
-// -------------------------------------------------------------------------
-bool ParseArgs(
- std::map< std::string, std::string >& args, int argc, char* argv[]
- )
+int main( int argc, char* argv[] )
{
- std::set< std::string > mandatory;
- mandatory.insert( "in" );
- mandatory.insert( "out" );
- mandatory.insert( "labels" );
-
- args[ "upper_threshold" ] = "-600";
-
- int i = 1;
- while( i < argc )
+ typedef TCLAP::ValueArg< std::string > _TStringArg;
+ typedef TCLAP::ValueArg< double > _TRealArg;
+ typedef TCLAP::ValueArg< TPixel > _TPixelArg;
+
+ // Parse input line
+ _TStringArg in( "i", "input", "Input image", true, "", "file" );
+ _TStringArg labels( "l", "labels", "Input labels", true, "", "file" );
+ _TStringArg vesselness( "v", "vesselness", "Input vesselness", true, "", "file" );
+ _TStringArg out( "o", "output", "Output image", true, "", "file" );
+ _TRealArg vThr( "a", "vesselness_thr", "Vesselness threshold (%)", false, 5, "value (5)" );
+ _TPixelArg uThr( "u", "upper_thr", "Upper threshold", false, -400, "value (-400)" );
+ try
{
- std::string cmd = argv[ i ] + 1;
- args[ cmd ] = argv[ i + 1 ];
- i += 2;
-
- } // elihw
-
- bool complete = true;
- for( std::string t: mandatory )
- complete &= ( args.find( t ) != args.end( ) );
-
- if( !complete )
+ TCLAP::CmdLine cmd( "Labelling", ' ', "1.0.0" );
+ cmd.add( uThr );
+ cmd.add( vThr );
+ cmd.add( out );
+ cmd.add( vesselness );
+ cmd.add( labels );
+ cmd.add( in );
+ cmd.parse( argc, argv );
+ }
+ catch( TCLAP::ArgException& err )
{
std::cerr
- << "Usage: " << argv[ 0 ] << std::endl
- << "\t-in filename" << std::endl
- << "\t-out filename" << std::endl
- << "\t-labels filename" << std::endl
- << "\t[-upper_threshold value]" << std::endl;
- return( false );
+ << "===============================" << std::endl
+ << "Error caught: " << std::endl
+ << err.error( ) << " " << err.argId( ) << std::endl
+ << "===============================" << std::endl
+ << std::endl;
+ return( 1 );
- } // fi
- return( true );
-}
+ } // yrt
-// -------------------------------------------------------------------------
-int main( int argc, char* argv[] )
-{
- std::map< std::string, std::string > args;
try
{
- if( ParseArgs( args, argc, argv ) )
- {
- // Read input image
- TImage::Pointer input_image;
- ReadImage( input_image, args[ "in" ] );
-
- // Read labels image
- TLabels::Pointer input_labels;
- ReadImage( input_labels, args[ "labels" ] );
-
- // Mori segmentation
- /* TODO
- typedef fpa::Filters::Image::Mori< TImage, TLabels > TMori;
- TMori::Pointer mori = TMori::New( );
- mori->SetInput( input_image );
- mori->SetSeed( seed );
- mori->SetInsideValue( 1 );
- mori->SetOutsideValue( 0 );
- mori->SetMinimumThreshold(
- TPixel( std::atof( args[ "minimum_threshold" ].c_str( ) ) )
- );
- mori->SetSignalKernelSize(
- std::atoi( args[ "signal_kernel_size" ].c_str( ) )
- );
- mori->SetSignalThreshold(
- std::atof( args[ "signal_threshold" ].c_str( ) )
- );
- mori->SetSignalInfluence(
- std::atof( args[ "signal_influence" ].c_str( ) )
- );
- mori->SetThresholds(
- TPixel( std::atof( args[ "lower_threshold" ].c_str( ) ) ),
- TPixel( std::atof( args[ "upper_threshold" ].c_str( ) ) ),
- TPixel( std::atof( args[ "delta_threshold" ].c_str( ) ) )
- );
- double t = MeasureTime( mori );
- std::cout << "Mori executed in " << t << " s" << std::endl;
- WriteImage( mori->GetOutput( ), args[ "out" ] );
-
- std::map< std::string, std::string >::const_iterator i =
- args.find( "out_signal" );
- if( i != args.end( ) )
- {
- std::stringstream signal;
- unsigned long nthr = mori->GetNumberOfEvaluatedThresholds( );
- signal << "# nThr = " << nthr << std::endl;
- signal << "# Opt = " << mori->GetOptimumThreshold( ) << std::endl;
- for( unsigned long j = 0; j < nthr; ++j )
- {
- typename TMori::TPeak p;
- double x, y;
- mori->GetSignalValues( j, x, y, p );
- signal << x << " " << y << std::endl;
-
- } // rof
- std::ofstream signals_str( i->second.c_str( ) );
- signals_str << signal.str( );
- signals_str.close( );
-
- } // fi
- */
- }
- else
- return( 1 );
+ // Read input image
+ TImage::Pointer input_image;
+ CTBronchi::ReadImage( input_image, in.getValue( ) );
+
+ // Read input labels
+ TLabels::Pointer input_labels;
+ CTBronchi::ReadImage( input_labels, labels.getValue( ) );
+
+ // Read input vesselness
+ TScalarImage::Pointer input_vesselness;
+ CTBronchi::ReadImage( input_vesselness, vesselness.getValue( ) );
+
+ // Create labels
+ typedef CTBronchi::MoriLabelling< TImage, TLabels, TScalarImage > _TLabelling;
+ _TLabelling::Pointer labelling = _TLabelling::New( );
+ labelling->SetInput( input_image );
+ labelling->SetInputLabels( input_labels );
+ labelling->SetInputVesselness( input_vesselness );
+ labelling->SetVesselnessThreshold( vThr.getValue( ) );
+ labelling->SetUpperThreshold( uThr.getValue( ) );
+ labelling->SetInsideValue( 1 );
+ labelling->SetOutsideValue( 2 );
+ labelling->SetFillValue( 2 );
+ double t = CTBronchi::MeasureTime( labelling );
+ std::cout << "Labelling executed in " << t << " s" << std::endl;
+
+ // Write result
+ CTBronchi::WriteImage( labelling->GetOutput( ), out.getValue( ) );
}
catch( std::exception& err )
{
std::cerr
<< "===============================" << std::endl
<< "Error caught: " << std::endl
- << err.what( )
+ << err.what( ) << std::endl
<< "===============================" << std::endl
<< std::endl;
return( 1 );