]> Creatis software - FrontAlgorithms.git/blobdiff - appli/CTBronchi/MoriLabelling.cxx
...
[FrontAlgorithms.git] / appli / CTBronchi / MoriLabelling.cxx
index 0915cd5f1cc0448a0b2dda4816cd96ed1cc68fda..0ad694d9027a1249f56f76c6b760c447fa499841 100644 (file)
+// =========================================================================
+// @author Leonardo Florez Valencia
+// @email florez-l@javeriana.edu.co
+// =========================================================================
+
 #include <chrono>
+#include <map>
 #include <itkImage.h>
 #include <itkImageFileReader.h>
 #include <itkImageFileWriter.h>
-#include <CTBronchi/MoriLabelling.h>
+#include <fpa/Filters/Image/Mori.h>
 
 // -------------------------------------------------------------------------
 const unsigned int Dim = 3;
-typedef short          TPixel;
-typedef unsigned short TLabel;
+typedef short         TPixel;
+typedef unsigned char TLabel;
+typedef itk::Image< TPixel, Dim > TImage;
+typedef itk::Image< TLabel, Dim > TLabels;
 
-typedef itk::Image< TPixel, Dim > TInputImage;
-typedef itk::Image< TLabel, Dim > TLabelImage;
+// -------------------------------------------------------------------------
+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( ) );
+}
 
-typedef CTBronchi::MoriLabelling< TInputImage, TLabelImage > TFilter;
+// -------------------------------------------------------------------------
+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( );
+}
 
 // -------------------------------------------------------------------------
-int main( int argc, char* argv[] )
+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[]
+  )
 {
-  // Get arguments
-  if( argc < 8 )
+  std::set< std::string > mandatory;
+  mandatory.insert( "in" );
+  mandatory.insert( "out" );
+  mandatory.insert( "labels" );
+
+  args[ "upper_threshold" ] = "-600";
+
+  int i = 1;
+  while( i < argc )
+  {
+    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 )
   {
     std::cerr
       << "Usage: " << argv[ 0 ] << std::endl
-      << "   input_image label_image output_image" << std::endl
-      << "   upper_threshold(-400)" << std::endl
-      << "   inside_value(255)" << std::endl
-      << "   inside_label(1) outside_label(2)"
-      << std::endl;
-    return( 1 );
+      << "\t-in filename" << std::endl
+      << "\t-out filename" << std::endl
+      << "\t-labels filename" << std::endl
+      << "\t[-upper_threshold value]" << std::endl;
+    return( false );
 
   } // fi
-  std::string input_image_filename = argv[ 1 ];
-  std::string label_image_filename = argv[ 2 ];
-  std::string output_image_filename = argv[ 3 ];
-  TLabel upper_threshold = std::atoi( argv[ 4 ] );
-  TLabel inside_value = std::atoi( argv[ 5 ] );
-  TLabel inside_label = std::atoi( argv[ 6 ] );
-  TLabel outside_label = std::atoi( argv[ 7 ] );
-
-  // Read images
-  itk::ImageFileReader< TInputImage >::Pointer input_image_reader =
-    itk::ImageFileReader< TInputImage >::New( );
-  input_image_reader->SetFileName( input_image_filename );
-
-  itk::ImageFileReader< TLabelImage >::Pointer label_image_reader =
-    itk::ImageFileReader< TLabelImage >::New( );
-  label_image_reader->SetFileName( label_image_filename );
-
-  // Prepare filter
-  TFilter::Pointer filter = TFilter::New( );
-  filter->SetInputLabelImage( label_image_reader->GetOutput( ) );
-  filter->SetInputRawImage( input_image_reader->GetOutput( ) );
-  filter->SetUpperThreshold( upper_threshold );
-  filter->SetInsideValue( inside_value );
-  filter->SetInsideLabel( inside_label );
-  filter->SetOutsideLabel( outside_label );
-
-  // Show some information
-  std::cout << "----------------------------------------------" << std::endl;
-  std::cout << "Image: " << input_image_filename << std::endl;
-
-  // Execute pipeline
-  std::chrono::time_point< std::chrono::high_resolution_clock > ts, te;
-  std::chrono::duration< double > tr;
-  try
-  {
-    ts = std::chrono::high_resolution_clock::now( );
-    input_image_reader->Update( );
-    te = std::chrono::high_resolution_clock::now( );
-    tr = te - ts;
-    std::cout << "Raw read time: " << tr.count( ) << " s" << std::endl;
-
-    ts = std::chrono::high_resolution_clock::now( );
-    label_image_reader->Update( );
-    te = std::chrono::high_resolution_clock::now( );
-    tr = te - ts;
-    std::cout << "Label read time: " << tr.count( ) << " s" << std::endl;
-
-    ts = std::chrono::high_resolution_clock::now( );
-    filter->Update( );
-    te = std::chrono::high_resolution_clock::now( );
-    tr = te - ts;
-    std::cout
-      << "Labelling time: " << tr.count( ) << " s" << std::endl;
-  }
-  catch( std::exception& err )
-  {
-    std::cerr << "Error caught: " << err.what( ) << std::endl;
-    return( 1 );
-
-  } // yrt
+  return( true );
+}
 
-  // Save output image
-  itk::ImageFileWriter< TLabelImage >::Pointer output_image_writer =
-    itk::ImageFileWriter< TLabelImage >::New( );
-  output_image_writer->SetInput( filter->GetOutput( ) );
-  output_image_writer->SetFileName( output_image_filename );
+// -------------------------------------------------------------------------
+int main( int argc, char* argv[] )
+{
+  std::map< std::string, std::string > args;
   try
   {
-    ts = std::chrono::high_resolution_clock::now( );
-    output_image_writer->Update( );
-    te = std::chrono::high_resolution_clock::now( );
-    tr = te - ts;
-    std::cout << "Write time: " << tr.count( ) << " s" << std::endl;
+    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 );
   }
   catch( std::exception& err )
   {
-    std::cerr << "Error caught: " << err.what( ) << std::endl;
+    std::cerr
+      << "===============================" << std::endl
+      << "Error caught: " << std::endl
+      << err.what( )
+      << "===============================" << std::endl
+      << std::endl;
     return( 1 );
 
   } // yrt
-  std::cout << "----------------------------------------------" << std::endl;
-
   return( 0 );
 }