]> Creatis software - FrontAlgorithms.git/commitdiff
...
authorLeonardo Flórez-Valencia <leonardo.florez@gmail.com>
Thu, 21 Sep 2017 16:53:32 +0000 (18:53 +0200)
committerLeonardo Flórez-Valencia <leonardo.florez@gmail.com>
Thu, 21 Sep 2017 16:53:32 +0000 (18:53 +0200)
12 files changed:
appli/CTBronchi/CMakeLists.txt
appli/CTBronchi/MoriLabelling.cxx
appli/CTBronchi/MoriLabelling.h
appli/CTBronchi/MoriLabelling.hxx
appli/CTBronchi/MoriSegmentation.cxx
lib/fpa/Common/OriginalRandomWalker.hxx
lib/fpa/Functors/Dijkstra/Image/Gaussian.h
tests/image/Dijkstra/Gaussian.cxx
tests/image/RandomWalker/CMakeLists.txt
tests/image/RandomWalker/CompareRandomWalkers.cxx [new file with mode: 0644]
tests/image/RandomWalker/Gaussian.cxx
tests/image/RandomWalker/Original.cxx

index 1851e55671088bd42244a923e619800c0344c165..fccfb975998a0d52e1095dcd6c0491555a9c3fd8 100644 (file)
@@ -14,7 +14,8 @@ if(fpa_BUILD_CTBronchi)
 set(_pfx fpa_CTBronchi_)
 set(
   _examples
-  Process
+  MoriSegmentation
+  MoriLabelling
   )
 foreach(_e ${_examples})
   add_executable(${_pfx}${_e} ${_e}.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 );
 }
 
index b5993105a70bf691cbae243d109f84b7272896c7..d07968b3ca670ac90fe9283c5c380f3705d34395 100644 (file)
@@ -86,7 +86,6 @@ namespace CTBronchi
 #ifndef ITK_MANUAL_INSTANTIATION
 #  include <CTBronchi/MoriLabelling.hxx>
 #endif // ITK_MANUAL_INSTANTIATION
-
 #endif // __CTBronchi__MoriLabelling__h__
 
 // eof - $RCSfile$
index b6f982a5f4f8e2b31cf542939d154d2be3d0f896..f786948c616f0d453872c092e7464f71bdd670f7 100644 (file)
@@ -2,7 +2,6 @@
 // @author Leonardo Florez Valencia
 // @email florez-l@javeriana.edu.co
 // =========================================================================
-
 #ifndef __CTBronchi__MoriLabelling__hxx__
 #define __CTBronchi__MoriLabelling__hxx__
 
@@ -107,7 +106,6 @@ namespace CTBronchi
   }
 
 } // ecapseman
-
 #endif // __CTBronchi__MoriLabelling__hxx__
 
 // eof - $RCSfile$
index 39b9e0dcb3bcec9978554fa19e2c6bdec5a8f35d..dcbae23addd5d556e37814759cf05673691874e9 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 <fpa/Image/Mori.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;
-typedef fpa::Image::Mori< TInputImage, TLabelImage > TFilter;
+// -------------------------------------------------------------------------
+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( ) );
+}
 
 // -------------------------------------------------------------------------
-int main( int argc, char* argv[] )
+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 )
 {
-  // Get arguments
-  if( argc < 16 )
+  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[]
+  )
+{
+  std::set< std::string > mandatory;
+  mandatory.insert( "in" );
+  mandatory.insert( "out" );
+  mandatory.insert( "seed" );
+
+  args[ "minimum_threshold" ] = "-850";
+  args[ "signal_kernel_size" ] = "20";
+  args[ "signal_threshold" ] = "100";
+  args[ "signal_influence" ] = "0.5";
+  args[ "lower_threshold" ] = "-1024";
+  args[ "upper_threshold" ] = "0";
+  args[ "delta_threshold" ] = "1";
+
+  int i = 1;
+  while( i < argc )
+  {
+    std::string cmd = argv[ i ] + 1;
+    if( cmd == "seed" )
+    {
+      std::stringstream seed;
+      seed
+        << argv[ i + 1 ] << ";"
+        << argv[ i + 2 ] << ";"
+        << argv[ i + 3 ];
+      args[ cmd ] = seed.str( );
+      i += 4;
+    }
+    else
+    {
+      args[ cmd ] = argv[ i + 1 ];
+      i += 2;
+
+    } // fi
+
+  } // 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 output_image output_signal" << std::endl
-      << "   init_threshold end_threshold delta" << std::endl
-      << "   minimum_threshold" << std::endl
-      << "   inside_value outside_value" << std::endl
-      << "   signal_kernel_size signal_threshold signal_influence"
-      << std::endl
-      << "   seed_x seed_y seed_z"
-      << std::endl;
-    return( 1 );
+      << "\t-in filename" << std::endl
+      << "\t-out filename" << std::endl
+      << "\t-seed x y z" << std::endl
+      << "\t[-out_signal filename]" << std::endl
+      << "\t[-minimum_threshold value]" << std::endl
+      << "\t[-signal_kernel_size value]" << std::endl
+      << "\t[-signal_threshold value]" << std::endl
+      << "\t[-signal_influence value]" << std::endl
+      << "\t[-lower_threshold value]" << std::endl
+      << "\t[-upper_threshold value]" << std::endl
+      << "\t[-delta_threshold value]" << std::endl;
+    return( false );
 
   } // fi
-  std::string input_image_filename = argv[ 1 ];
-  std::string output_image_filename = argv[ 2 ];
-  std::string output_signal_filename = argv[ 3 ];
-  TPixel init_threshold = std::atoi( argv[ 4 ] );
-  TPixel end_threshold = std::atoi( argv[ 5 ] );
-  TPixel delta = std::atoi( argv[ 6 ] );
-  TPixel minimum_threshold = std::atoi( argv[ 7 ] );
-  TLabel inside_value = std::atoi( argv[ 8 ] );
-  TLabel outside_value = std::atoi( argv[ 9 ] );
-  unsigned long signal_kernel_size = std::atoi( argv[ 10 ] );
-  double signal_threshold = std::atof( argv[ 11 ] );
-  double signal_influence = std::atof( argv[ 12 ] );
-  TInputImage::PointType pseed;
-  for( int i = 0; i < Dim; ++i )
-    pseed[ i ] = std::atof( argv[ 13 + i ] );
-
-  // Read image
-  itk::ImageFileReader< TInputImage >::Pointer input_image_reader =
-    itk::ImageFileReader< TInputImage >::New( );
-  input_image_reader->SetFileName( input_image_filename );
-
-  // Prepare filter
-  TFilter::Pointer filter = TFilter::New( );
-  filter->SetInput( input_image_reader->GetOutput( ) );
-  filter->SetSeed( pseed );
-  filter->SetThresholds( init_threshold, end_threshold, delta );
-  filter->SetMinimumThreshold( minimum_threshold );
-  filter->SetInsideValue( inside_value );
-  filter->SetOutsideValue( outside_value );
-  filter->SetSignalKernelSize( signal_kernel_size );
-  filter->SetSignalThreshold( signal_threshold );
-  filter->SetSignalInfluence( signal_influence );
-
-  // 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 << "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
-      << "Mori time: " << tr.count( ) << " s" << std::endl
-      << "Optimum threshold: " << filter->GetOptimumThreshold( ) << std::endl
-      << "Number of thresholds: "
-      << filter->GetNumberOfEvaluatedThresholds( ) << 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->GetThresholdedOutput( ) );
-  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 ) )
+    {
+      // Parse seed
+      TImage::PointType seed;
+      char* str = new char[ args[ "seed" ].size( ) + 1 ];
+      strcpy( str, args[ "seed" ].c_str( ) );
+      seed[ 0 ] = std::atof( strtok( str, ";" ) );
+      seed[ 1 ] = std::atof( strtok( NULL, ";" ) );
+      seed[ 2 ] = std::atof( strtok( NULL, ";" ) );
+
+      // Read input image
+      TImage::Pointer input_image;
+      ReadImage( input_image, args[ "in" ] );
+
+      // Mori segmentation
+      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
-
-  // Save output signal
-  std::ofstream osignal( output_signal_filename.c_str( ) );
-  for(
-    unsigned long i = 0; i < filter->GetNumberOfEvaluatedThresholds( ); ++i
-    )
-  {
-    double x, y;
-    TFilter::TPeak p;
-    filter->GetSignalValues( i, x, y, p );
-    osignal << x << " " << y << std::endl;
-
-  } // rof
-  osignal.close( );
-  std::cout << "----------------------------------------------" << std::endl;
-
   return( 0 );
 }
 
index 4b60c1bb65302529b9ccf0d304f71b2c126210aa..46dd9354db02015ad64fdd718db5ea37dfd929b1 100644 (file)
@@ -5,7 +5,6 @@
 #ifndef __fpa__Common__OriginalRandomWalker__hxx__
 #define __fpa__Common__OriginalRandomWalker__hxx__
 
-#include <cmath>
 #include <itkImageRegionConstIteratorWithIndex.h>
 #include <itkImageRegionIteratorWithIndex.h>
 #include <Eigen/Sparse>
@@ -247,7 +246,7 @@ _Laplacian( _TTriplets& A, _TTriplets& R, const _TTriplets& B )
   thrStr.B = reinterpret_cast< const void* >( &B );
 
   // Configure threader
-  const TImage* in = this->GetInputLabels( );
+  const TImage* in = this->GetInput( );
   const itk::ImageRegionSplitterBase* split = this->GetImageRegionSplitter( );
   const unsigned int nThreads =
     split->GetNumberOfSplits(
index 2effb513f4f707236c3203be83514168b9700420..b819f01981b92c9971438c544fe0f577c7b7e3a3 100644 (file)
@@ -63,7 +63,7 @@ namespace fpa
                 d       -= TValue( image->GetPixel( p ) );
                 d       /= this->m_Beta;
                 d       *= d;
-                if( this->m_TreatAsWeight ) d = std::exp(  d );
+                if( this->m_TreatAsWeight ) d = std::exp(  d ) - TValue( 1 );
                 else                        d = std::exp( -d );
 
                 if( d < this->m_Epsilon ) return( this->m_Epsilon );
index 3139e836c8ddb925ba897072c51f60985901b626..ef6d8274e5f706c69f2fec1fe221598f1cf3268b 100644 (file)
@@ -24,7 +24,8 @@ int main( int argc, char* argv[] )
   {
     std::cerr
       << "Usage: " << argv[ 0 ]
-      << " input_image output_image output_marks output_mst alpha beta [seeds]"
+      << " input_image output_image output_marks output_mst"
+      << " beta epsilon [seeds]"
       << std::endl;
     return( 1 );
 
@@ -33,8 +34,8 @@ int main( int argc, char* argv[] )
   std::string output_image_filename = argv[ 2 ];
   std::string output_marks_filename = argv[ 3 ];
   std::string output_mst_filename = argv[ 4 ];
-  double alpha = std::atof( argv[ 5 ] );
-  double beta = std::atof( argv[ 6 ] );
+  double beta = std::atof( argv[ 5 ] );
+  double eps = std::atof( argv[ 6 ] );
 
   // Read image
   typedef itk::ImageFileReader< TInputImage > TInputImageReader;
@@ -44,8 +45,8 @@ int main( int argc, char* argv[] )
   // Prepare weight functor
   typedef fpa::Functors::Dijkstra::Image::Gaussian< TInputImage, TOutputPixel > TWeight;
   TWeight::Pointer weight = TWeight::New( );
-  weight->SetAlpha( alpha );
   weight->SetBeta( beta );
+  weight->SetEpsilon( eps );
 
   // Prepare filter
   typedef fpa::Filters::Image::Dijkstra< TInputImage, TOutputImage > TFilter;
index 859b3a6aaffd196fda90d81562c216a4dad528a5..e0aef14528fa2fbaeb149c6a217f741f2a4f8ad2 100644 (file)
@@ -8,6 +8,7 @@ set(
   _tests
   Gaussian
   Original
+  CompareRandomWalkers
   )
 include_directories(${PROJECT_SOURCE_DIR}/lib ${PROJECT_BINARY_DIR}/lib)
 
diff --git a/tests/image/RandomWalker/CompareRandomWalkers.cxx b/tests/image/RandomWalker/CompareRandomWalkers.cxx
new file mode 100644 (file)
index 0000000..67afb48
--- /dev/null
@@ -0,0 +1,192 @@
+// =========================================================================
+// @author Leonardo Florez Valencia
+// @email florez-l@javeriana.edu.co
+// =========================================================================
+
+#include <chrono>
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <itkImage.h>
+#include <itkImageFileReader.h>
+#include <itkImageFileWriter.h>
+
+#include <fpa/Common/OriginalRandomWalker.h>
+#include <fpa/Filters/Image/RandomWalker.h>
+#include <fpa/Functors/Dijkstra/Image/Gaussian.h>
+
+// -------------------------------------------------------------------------
+const unsigned int Dim = 3;
+typedef short         TPixel;
+typedef unsigned char TLabel;
+typedef double        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 itk::ImageFileReader< typename _TImagePtr::ObjectType > _TReader;
+  typename _TReader::Pointer r = _TReader::New( );
+  r->SetFileName( fname );
+  double t = MeasureTime( r );
+  image = r->GetOutput( );
+  image->DisconnectPipeline( );
+  std::cout << "Image \"" << fname << "\" read in " << t << "s" << std::endl;
+}
+
+// -------------------------------------------------------------------------
+template< class _TImagePtr >
+void WriteImage( const _TImagePtr& image, const std::string& fname )
+{
+  typedef itk::ImageFileWriter< typename _TImagePtr::ObjectType > _TWriter;
+  typename _TWriter::Pointer w = _TWriter::New( );
+  w->SetInput( image );
+  w->SetFileName( fname );
+  double t = MeasureTime( w );
+  std::cout << "Image \"" << fname << "\" written in " << t << "s" << std::endl;
+}
+
+// -------------------------------------------------------------------------
+template< class _TImagePtr, class _TLabelsPtr, class _TScalarImagePtr >
+void Original(
+  _TLabelsPtr& output_labels,
+  _TScalarImagePtr& output_probabilities,
+  const _TImagePtr& input,
+  const _TLabelsPtr& labels,
+  const typename _TScalarImagePtr::ObjectType::PixelType& beta,
+  const typename _TScalarImagePtr::ObjectType::PixelType& epsilon
+  )
+{
+  typedef typename _TImagePtr::ObjectType       _TImage;
+  typedef typename _TLabelsPtr::ObjectType      _TLabels;
+  typedef typename _TScalarImagePtr::ObjectType _TScalarImage;
+  typedef typename _TScalarImage::PixelType     _TScalar;
+  typedef
+    fpa::Functors::Dijkstra::Image::Gaussian< _TImage, _TScalar >
+    _TFunction;
+  typedef
+    fpa::Common::OriginalRandomWalker< _TImage, _TLabels, _TScalar >
+    _TFilter;
+
+  // Random walker function
+  typename _TFunction::Pointer edge = _TFunction::New( );
+  edge->SetBeta( beta );
+  edge->SetEpsilon( epsilon );
+  edge->TreatAsWeightOff( );
+
+  // Random walker
+  typename _TFilter::Pointer filter = _TFilter::New( );
+  filter->SetInput( input );
+  filter->SetInputLabels( labels );
+  filter->SetEdgeFunction( edge );
+  double t = MeasureTime( filter );
+  output_labels = filter->GetOutput( );
+  output_probabilities = filter->GetOutputProbabilities( );
+  output_labels->DisconnectPipeline( );
+  output_probabilities->DisconnectPipeline( );
+  std::cout << "Original RW executed in " << t << "s" << std::endl;
+}
+
+// -------------------------------------------------------------------------
+template< class _TImagePtr, class _TLabelsPtr, class _TScalarImagePtr >
+void FrontPropagation(
+  _TLabelsPtr& output_labels,
+  _TScalarImagePtr& output_probabilities,
+  const _TImagePtr& input,
+  const _TLabelsPtr& labels,
+  const typename _TScalarImagePtr::ObjectType::PixelType& beta,
+  const typename _TScalarImagePtr::ObjectType::PixelType& epsilon
+  )
+{
+  typedef typename _TImagePtr::ObjectType       _TImage;
+  typedef typename _TLabelsPtr::ObjectType      _TLabels;
+  typedef typename _TScalarImagePtr::ObjectType _TScalarImage;
+  typedef typename _TScalarImage::PixelType     _TScalar;
+  typedef
+    fpa::Functors::Dijkstra::Image::Gaussian< _TImage, _TScalar >
+    _TFunction;
+  typedef
+    fpa::Filters::Image::RandomWalker< _TImage, _TLabels, _TScalar >
+    _TFilter;
+
+  // Random walker function
+  typename _TFunction::Pointer edge = _TFunction::New( );
+  edge->SetBeta( beta );
+  edge->SetEpsilon( epsilon );
+  edge->TreatAsWeightOn( );
+
+  // Random walker
+  typename _TFilter::Pointer filter = _TFilter::New( );
+  filter->SetInputImage( input );
+  filter->SetInputLabels( labels );
+  filter->SetWeightFunction( edge );
+  double t = MeasureTime( filter );
+  output_labels = filter->GetOutputLabels( );
+  output_probabilities = filter->GetOutputCosts( );
+  output_labels->DisconnectPipeline( );
+  output_probabilities->DisconnectPipeline( );
+  std::cout << "Front propagation RW executed in " << t << "s" << std::endl;
+}
+
+// -------------------------------------------------------------------------
+int main( int argc, char* argv[] )
+{
+  // Get input arguments
+  if( argc < 5 )
+  {
+    std::cerr
+      << "Usage: " << argv[ 0 ]
+      << " input_image input_labels beta epsilon"
+      << std::endl;
+    return( 1 );
+
+  } // fi
+  std::string input_image_filename = argv[ 1 ];
+  std::string input_labels_filename = argv[ 2 ];
+  std::istringstream values( std::string( argv[ 3 ] ) + " " + argv[ 4 ] );
+  TScalar beta, epsilon;
+  values >> beta >> epsilon;
+
+  // Read inputs
+  TImage::Pointer input_image;
+  TLabels::Pointer input_labels;
+  ReadImage( input_image, input_image_filename );
+  ReadImage( input_labels, input_labels_filename );
+
+  // Execute algorithms
+  TLabels::Pointer original_labels, fp_labels;
+  TScalarImage::Pointer original_probabilities, fp_probabilities;
+  Original(
+    original_labels, original_probabilities,
+    input_image, input_labels,
+    beta, epsilon
+    );
+  FrontPropagation(
+    fp_labels, fp_probabilities,
+    input_image, input_labels,
+    beta, epsilon
+    );
+
+  WriteImage( original_labels, "orig.png" );
+  WriteImage( fp_labels, "fp.png" );
+
+  return( 0 );
+}
+
+// eof - $RCSfile$
index 82d44aeddee6aca97825a7352852db4ddb4da684..5c2c798d60cc81c9a3ff9e0847b4649825794b9e 100644 (file)
@@ -25,7 +25,7 @@ int main( int argc, char* argv[] )
   {
     std::cerr
       << "Usage: " << argv[ 0 ]
-      << " input_image input_label output_image output_costs alpha beta"
+      << " input_image input_label output_image output_costs beta epsilon"
       << std::endl;
     return( 1 );
 
@@ -34,8 +34,8 @@ int main( int argc, char* argv[] )
   std::string input_label_filename = argv[ 2 ];
   std::string output_image_filename = argv[ 3 ];
   std::string output_costs_filename = argv[ 4 ];
-  double alpha = std::atof( argv[ 5 ] );
-  double beta = std::atof( argv[ 6 ] );
+  double beta = std::atof( argv[ 5 ] );
+  double eps = std::atof( argv[ 6 ] );
 
   // Read image
   typedef itk::ImageFileReader< TImage > TImageReader;
@@ -50,8 +50,8 @@ int main( int argc, char* argv[] )
   // Prepare weight functor
   typedef fpa::Functors::Dijkstra::Image::Gaussian< TImage, TCost > TWeight;
   TWeight::Pointer weight = TWeight::New( );
-  weight->SetAlpha( alpha );
   weight->SetBeta( beta );
+  weight->SetEpsilon( eps );
 
   // Prepare filter
   typedef fpa::Filters::Image::RandomWalker< TImage, TLabelImage, TCost > TFilter;
index 0ea2bd0c652ae7043d56e76f01e75f8cf0eb8092..4bc9fbef8d8293175586550a516ea21eaa20616e 100644 (file)
@@ -22,12 +22,12 @@ typedef itk::Image< TLabel, Dim > TLabels;
 int main( int argc, char* argv[] )
 {
   // Get input arguments
-  if( argc < 8 )
+  if( argc < 7 )
   {
     std::cerr
       << "Usage: " << argv[ 0 ]
       << " input_image input_labels output_labels output_probabilities"
-      << " beta eps normalize"
+      << " beta eps"
       << std::endl;
     return( 1 );
 
@@ -35,14 +35,12 @@ int main( int argc, char* argv[] )
   std::string input_image_filename, input_labels_filename;
   std::string output_labels_filename, output_probabilities_filename;
   double beta, eps;
-  bool normalize;
   input_image_filename = argv[ 1 ];
   input_labels_filename = argv[ 2 ];
   output_labels_filename = argv[ 3 ];
   output_probabilities_filename = argv[ 4 ];
   beta = std::atof( argv[ 5 ] );
   eps = std::atof( argv[ 6 ] );
-  normalize = ( argv[ 7 ][ 0 ] == '1' );
 
   // Read image
   typedef itk::ImageFileReader< TImage > TImageReader;