]> Creatis software - FrontAlgorithms.git/commitdiff
Some visualizaton added
authorLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Fri, 20 Feb 2015 22:47:40 +0000 (17:47 -0500)
committerLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Fri, 20 Feb 2015 22:47:40 +0000 (17:47 -0500)
appli/examples/example_ImageAlgorithmRegionGrow_MultipleThresholds.cxx
lib/fpa/Base/Algorithm.hxx
lib/fpa/Image/RegionGrowWithMultipleThresholds.h
lib/fpa/Image/RegionGrowWithMultipleThresholds.hxx
lib/fpa/VTK/Image3DObserver.hxx
lib/fpa/VTK/ImageMPR.cxx
lib/fpa/VTK/ImageMPR.h

index deef6de805a8e711164b8313a4e4319905a30614..e9b06cc7238e475e0ac725120740153abc752cde 100644 (file)
@@ -1,3 +1,4 @@
+#include <ctime>
 #include <iostream>
 #include <limits>
 #include <set>
@@ -5,9 +6,11 @@
 
 #include <itkImage.h>
 #include <itkImageFileReader.h>
+#include <itkImageFileWriter.h>
 #include <itkImageToVTKImageFilter.h>
 
 #include <vtkImageActor.h>
+#include <vtkImageMarchingCubes.h>
 #include <vtkProperty.h>
 #include <vtkRenderer.h>
 #include <vtkRenderWindow.h>
@@ -27,6 +30,7 @@ typedef itk::Image< TPixel, Dim >  TImage;
 typedef itk::ImageToVTKImageFilter< TImage > TVTKImage;
 
 typedef itk::ImageFileReader< TImage >   TImageReader;
+typedef itk::ImageFileWriter< TImage >   TImageWriter;
 
 typedef
 fpa::Image::RegionGrowWithMultipleThresholds< TImage >
@@ -38,11 +42,13 @@ TObserver;
 // -------------------------------------------------------------------------
 int main( int argc, char* argv[] )
 {
-  if( argc < 5 )
+  if( argc < 6 )
   {
     std::cerr
       << "Usage: " << argv[ 0 ]
-      << " input_image thr_0 thr_1 step" << std::endl;
+      << " input_image thr_0 thr_1 step output_image"
+      << " visual_debug"
+      << std::endl;
     return( 1 );
 
   } // fi
@@ -50,6 +56,10 @@ int main( int argc, char* argv[] )
   TPixel thr_0 = TPixel( std::atof( argv[ 2 ] ) );
   TPixel thr_1 = TPixel( std::atof( argv[ 3 ] ) );
   unsigned int step = std::atoi( argv[ 4 ] );
+  std::string output_image_fn = argv[ 5 ];
+  bool visual_debug = false;
+  if( argc > 6 )
+    visual_debug = ( std::atoi( argv[ 6 ] ) == 1 );
   
   // Read image
   TImageReader::Pointer input_image_reader = TImageReader::New( );
@@ -93,23 +103,63 @@ int main( int argc, char* argv[] )
   TImage::IndexType seed_idx;
   input_image->TransformPhysicalPointToIndex( seed_pnt, seed_idx );
 
-  // Configure observer
-  TObserver::Pointer obs = TObserver::New( );
-  obs->SetImage( input_image, view.GetWindow( ) );
-
   // Configure algorithm
   TFrontAlgorithm::Pointer algorithm = TFrontAlgorithm::New( );
   algorithm->AddThresholds( thr_0, thr_1, step );
   algorithm->AddSeed( seed_idx, 0 );
-  algorithm->AddObserver( itk::AnyEvent( ), obs );
-  algorithm->ThrowEventsOn( );
   algorithm->SetInput( input_image );
   algorithm->SetNeighborhoodOrder( 1 );
   algorithm->SetDifferenceThreshold( double( 3 ) );
+
+  if( visual_debug )
+  {
+    // Configure observer
+    TObserver::Pointer obs = TObserver::New( );
+    obs->SetImage( input_image, view.GetWindow( ) );
+    algorithm->AddObserver( itk::AnyEvent( ), obs );
+    algorithm->ThrowEventsOn( );
+  }
+  else
+    algorithm->ThrowEventsOff( );
+
+  std::clock_t start = std::clock( );
   algorithm->Update( );
+  std::clock_t end = std::clock( );
+  double seconds = double( end - start ) / double( CLOCKS_PER_SEC );
+  std::cout << "Execution time = " << seconds << std::endl;
+
+  // Show result
+  TVTKImage::Pointer output_image_vtk = TVTKImage::New( );
+  output_image_vtk->SetInput( algorithm->GetOutput( ) );
+  output_image_vtk->Update( );
+
+  vtkSmartPointer< vtkImageMarchingCubes > mc =
+    vtkSmartPointer< vtkImageMarchingCubes >::New( );
+  mc->SetInputData( output_image_vtk->GetOutput( ) );
+  mc->SetValue(
+    0,
+    double( algorithm->GetInsideValue( ) ) * double( 0.95 )
+    );
+  mc->Update( );
 
   // Let some interaction and close program
+  view.AddPolyData( mc->GetOutput( ), 0.1, 0.6, 0.8, 0.5 );
   view.Start( );
+
+  // Write resulting image
+  TImageWriter::Pointer output_image_writer = TImageWriter::New( );
+  output_image_writer->SetInput( algorithm->GetOutput( ) );
+  output_image_writer->SetFileName( output_image_fn );
+  try
+  {
+    output_image_writer->Update( );
+  }
+  catch( itk::ExceptionObject& err )
+  {
+    std::cerr << "Error caught: " << err << std::endl;
+    return( 1 );
+
+  } // yrt
   return( 0 );
 }
 
index f4ccfdb5c95ae0bdce26cac35df7869783fa7ef8..a58f2a6a047366e2896a302109a6eb887095711d 100644 (file)
@@ -95,6 +95,7 @@ GenerateData( )
   this->_InitializeQueue( );
   this->_Loop( );
   this->_AfterMainLoop( );
+  this->InvokeEvent( TEndEvent( ) );
 }
 
 // -------------------------------------------------------------------------
@@ -187,7 +188,6 @@ _Loop( )
     } // fi
 
   } // elihw
-  this->InvokeEvent( TEndEvent( ) );
   this->_AfterLoop( );
 }
 
index 409b18bb90895d8e65c4a70b303f604f6be3891e..3bcc7db7abe21be0dd71ff75f491b0cf396c47da 100644 (file)
@@ -40,7 +40,12 @@ namespace fpa
       itkNewMacro( Self );
       itkTypeMacro( RegionGrowWithMultipleThresholds, RegionGrow );
 
+      itkGetConstMacro( InsideValue, TPixel );
+      itkGetConstMacro( OutsideValue, TPixel );
       itkGetConstMacro( DifferenceThreshold, double );
+
+      itkSetMacro( InsideValue, TPixel );
+      itkSetMacro( OutsideValue, TPixel );
       itkSetMacro( DifferenceThreshold, double );
 
     public:
@@ -68,6 +73,8 @@ namespace fpa
 
     protected:
       TThresholds m_Thresholds;
+      TPixel m_InsideValue;
+      TPixel m_OutsideValue;
       double m_DifferenceThreshold;
       THistogram m_Histogram;
       unsigned long m_TotalCount;
index 387a833bcab9bda89bba8d05e36eb9ec76fdeeae..e9ea4566b6ef59791d567f8d86b5a14f659e51aa 100644 (file)
@@ -2,6 +2,7 @@
 #define __FPA__IMAGE__REGIONGROWWITHMULTIPLETHRESHOLDS__HXX__
 
 #include <limits>
+#include <itkBinaryThresholdImageFilter.h>
 #include <itkConstNeighborhoodIterator.h>
 
 // -------------------------------------------------------------------------
@@ -27,6 +28,8 @@ template< class I >
 fpa::Image::RegionGrowWithMultipleThresholds< I >::
 RegionGrowWithMultipleThresholds( )
   : Superclass( ),
+    m_InsideValue( TPixel( 1 ) ),
+    m_OutsideValue( TPixel( 0 ) ),
     m_DifferenceThreshold( double( 3 ) ),
     m_TotalCount( 0 ),
     m_LastDiff( double( 0 ) ),
@@ -108,6 +111,26 @@ template< class I >
 void fpa::Image::RegionGrowWithMultipleThresholds< I >::
 _AfterMainLoop( )
 {
+  typedef itk::BinaryThresholdImageFilter< I, I > _TBinFilter;
+
+  // Binarize, inplace, the grown region
+  if( this->m_Histogram.size( ) > 1 )
+  {
+    typename _TBinFilter::Pointer bin = _TBinFilter::New( );
+    bin->SetInput( this->GetOutput( ) );
+    bin->SetInsideValue( this->m_InsideValue );
+    bin->SetOutsideValue( this->m_OutsideValue );
+    bin->InPlaceOn( );
+    if( this->m_StopForced )
+      bin->SetUpperThreshold( this->m_StopThreshold );
+    else
+      bin->SetUpperThreshold( this->m_Histogram.rbegin( )->first );
+    bin->GraftOutput( this->GetOutput( ) );
+    bin->Update( );
+    this->GraftOutput( bin->GetOutput( ) );
+
+  } // fi
+
   this->Superclass::_AfterMainLoop( );
 }
 
index e2986229a821f687e205eeb82f9fc7acac8c10d6..e79bbbe24dc5e99e3badf6febd3fea399042193e 100644 (file)
@@ -167,7 +167,7 @@ Execute( const itk::Object* c, const itk::EventObject& e )
       {
         vtkRenderer* ren =
           this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
-        // TODO: ren->RemoveActor( this->m_PolyDataActor );
+        ren->RemoveActor( this->m_PolyDataActor );
         this->Render( );
 
       } // fi
index fc5883e6f724aa0c307e4c47930bcba921ad7355..30033958d05e4cf6f3ac6c67d5704720a1fd42f2 100644 (file)
@@ -214,7 +214,7 @@ SetSize( unsigned int w, unsigned int h )
 
 // -------------------------------------------------------------------------
 void fpa::VTK::ImageMPR::
-AddPolyData( vtkPolyData* pd, double r, double g, double b )
+AddPolyData( vtkPolyData* pd, double r, double g, double b, double opacity )
 {
   unsigned int i = this->m_PolyDatas.size( );
 
@@ -223,8 +223,10 @@ AddPolyData( vtkPolyData* pd, double r, double g, double b )
   this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
 
   this->m_Mappers[ i ]->SetInputData( pd );
+  this->m_Mappers[ i ]->ScalarVisibilityOff( );
   this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
   this->m_Actors[ i ]->GetProperty( )->SetColor( r, g, b );
+  this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
   this->m_Renderer->AddActor( this->m_Actors[ i ] );
 }
 
index ba82b9571fa4008c899025a6bc99467552087523..0ada0bb91f6e4328ba076b203d8035b71f631f44 100644 (file)
@@ -37,7 +37,10 @@ namespace fpa
       void SetBackground( double r, double g, double b );
       void SetSize( unsigned int w, unsigned int h );
 
-      void AddPolyData( vtkPolyData* pd, double r, double g, double b );
+      void AddPolyData(
+        vtkPolyData* pd,
+        double r, double g, double b, double opacity = double( 1 )
+        );
 
       unsigned int GetNumberOfSeeds( ) const;
       void GetSeed( int n, double* s ) const;