[Insight-users] Debug error with Fast marching filter! again

Luis Ibanez luis.ibanez at kitware.com
Mon Jan 15 13:31:52 EST 2007


Hi Zheying,

I tried your code without modifications, and it seems to run
fine until it gets to writing the outputs.

In your code you have hard-coded the filenames of several
output files. They are all PNG files. However, your images
are 3D. The PNG file format *does not* support 3D images,
therefore the writer is throwing an exception with the
message that states this problem.


Unfortunately you missed to add try/catch blocks around
the Update() calls of the writers and for that reason
you are not seeing the messages.


When adding the try/catch blocks I get the following
messages:


> ibanez at DARWINIA ~/src/UsersITK/WuZheying/bin $ ./fastMarching.exe
> brainweb165a10f17.mha  FastMarchingOP.mhd 140 122 0.5 -0.5 4 420 420
> 72 seedPosition [140, 122, 72]
> 
> itk::ExceptionObject (0xfd76d0) Location: "virtual void
> itk::PNGImageIO::Write(const void*)" File:
> /home/ibanez/src/Insight/Code/IO/itkPNGImageIO.cxx Line: 432 
> Description: itk::ERROR: PNGImageIO(0x8f9d50): PNG Writer can only
> write 2-dimensional images. You are requesting to write an image of
> dimension = 3 with filename FastMarchingFilterOutput1.png
> 
> 
> 
> ibanez at DARWINIA ~/src/UsersITK/WuZheying/bin $
> 

That shows you the importance of using try/catch blocks around
Update() calls.

Please read the ITK Software Guide:


     http://www.itk.org/ItkSoftwareGuide.pdf

In particular the chapter "Reading and Writing Images".
You will find many examples on how to use the try/catch
blocks for catching Exceptions.


When replacing the .png filenames with ".mhd" extensions
the code seems to be running fine.


Please find attached the a modified version of your code
that is working fine.


    Luis


----------------------
Wu Zheying wrote:
> Hi all,
> 
> I've posted a previous mail on the debug error when doing 3d 
> segmentation using fast marching. I am posting the code here. Can anyone 
> tell me what has gone wrong? I suspect its the way i am providing the 3d 
> coordinates in the command line...need some help here as i've been 
> figuring this for days! Thanks.
> 
> i am using the following command line inputs:
> fastmarchingimagefilter "MRI2edit.hdr" "FastMarchingOP.hdr" 140 122 0.5 
> -0.5 4 420 420 72
> 
> so my coordinates are (140,122,72) and my sigma is 0.5, alpha=-0.5, 
> beta=4, timethreshold & stopping value =420.
> 
> rgds,
> Zheying
> 
> code attached:
> 
> 
> 
> #if defined(_MSC_VER)
> #pragma warning ( disable : 4786 )
> #endif
> 
> #ifdef __BORLANDC__
> #define ITK_LEAN_AND_MEAN
> #endif
> 
> 
> 
> #include "itkCurvatureAnisotropicDiffusionImageFilter.h"
> #include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
> #include "itkSigmoidImageFilter.h"
> #include "itkImage.h"
> #include "itkFastMarchingImageFilter.h"
> #include "itkBinaryThresholdImageFilter.h"
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkRescaleIntensityImageFilter.h"
> 
> 
> int main( int argc, char *argv[] )
> {
>   if( argc < 11 )
>     {
>     std::cerr << "Missing Parameters " << std::endl;
>     std::cerr << "Usage: " << argv[0];
>     std::cerr << " inputImage  outputImage seedX seedY";
>     std::cerr << " Sigma SigmoidAlpha SigmoidBeta TimeThreshold 
> StoppingValue seedZ" << std::endl;
>     return 1;
>     }
> 
>  
>   typedef   float           InternalPixelType;
>   const     unsigned int    Dimension = 3;
>   typedef itk::Image< InternalPixelType, Dimension >  InternalImageType;
>  
> 
>  
>   typedef unsigned char OutputPixelType;
>   typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
> 
>   typedef itk::BinaryThresholdImageFilter< InternalImageType,
>                         OutputImageType    >    ThresholdingFilterType;
>   ThresholdingFilterType::Pointer thresholder = 
> ThresholdingFilterType::New();
> 
>   const InternalPixelType  timeThreshold = atof( argv[8] );
>  
>   thresholder->SetLowerThreshold(           0.0  );
>   thresholder->SetUpperThreshold( timeThreshold  );
> 
>   thresholder->SetOutsideValue(  0  );
>   thresholder->SetInsideValue(  255 );
> 
>   typedef  itk::ImageFileReader< InternalImageType > ReaderType;
>   typedef  itk::ImageFileWriter<  OutputImageType  > WriterType;
> 
>   ReaderType::Pointer reader = ReaderType::New();
>   WriterType::Pointer writer = WriterType::New();
> 
>   reader->SetFileName( argv[1] );
>   writer->SetFileName( argv[2] );
> 
>   typedef itk::RescaleIntensityImageFilter<
>                                InternalImageType,
>                                OutputImageType >   CastFilterType;
> 
>   typedef   itk::CurvatureAnisotropicDiffusionImageFilter<
>                                InternalImageType,
>                                InternalImageType>  SmoothingFilterType;
>  
>   SmoothingFilterType::Pointer smoothing = SmoothingFilterType::New();
> 
>   typedef   itk::GradientMagnitudeRecursiveGaussianImageFilter<
>                                InternalImageType,
>                                InternalImageType>  GradientFilterType;
> 
>   typedef   itk::SigmoidImageFilter<                              
>                                InternalImageType,
>                                InternalImageType>  SigmoidFilterType;
> 
>   GradientFilterType::Pointer  gradientMagnitude = 
> GradientFilterType::New();
>   SigmoidFilterType::Pointer sigmoid = SigmoidFilterType::New();
> 
>   sigmoid->SetOutputMinimum(  0.0  );
>   sigmoid->SetOutputMaximum(  1.0  );
> 
>   typedef  itk::FastMarchingImageFilter< InternalImageType,
>                               InternalImageType >    FastMarchingFilterType;
> 
>   FastMarchingFilterType::Pointer  fastMarching = 
> FastMarchingFilterType::New();
> 
>   smoothing->SetInput( reader->GetOutput() );
>   gradientMagnitude->SetInput( smoothing->GetOutput() );
>   sigmoid->SetInput( gradientMagnitude->GetOutput() );
>   fastMarching->SetInput( sigmoid->GetOutput() );
>   thresholder->SetInput( fastMarching->GetOutput() );
>   writer->SetInput( thresholder->GetOutput() );
> 
>   smoothing->SetTimeStep( 0.0625 );
>   smoothing->SetNumberOfIterations(  1 );
>   smoothing->SetConductanceParameter( 9.0 );
> 
> 
>   const double sigma = atof( argv[5] );
> 
>   gradientMagnitude->SetSigma(  sigma  );
> 
> 
>   const double alpha =  atof( argv[6] );
>   const double beta  =  atof( argv[7] );
> 
>   sigmoid->SetAlpha( alpha );
>   sigmoid->SetBeta(  beta  );
> 
>   typedef FastMarchingFilterType::NodeContainer           NodeContainer;
>   typedef FastMarchingFilterType::NodeType                NodeType;
>   NodeContainer::Pointer seeds = NodeContainer::New();
>  
> 
>   InternalImageType::IndexType  seedPosition;
>  
>   seedPosition[0] = atoi( argv[3] );
>   seedPosition[1] = atoi( argv[4] );
>   seedPosition[2] = atoi( argv[10] );
> 
>   NodeType node;
>   const double seedValue = 0.0;
>  
>   node.SetValue( seedValue );
>   node.SetIndex( seedPosition );
> 
>   seeds->Initialize();
>   seeds->InsertElement( 0, node );
> 
>   fastMarching->SetTrialPoints(  seeds  );
> 
>   CastFilterType::Pointer caster1 = CastFilterType::New();
>   CastFilterType::Pointer caster2 = CastFilterType::New();
>   CastFilterType::Pointer caster3 = CastFilterType::New();
>   CastFilterType::Pointer caster4 = CastFilterType::New();
> 
>   WriterType::Pointer writer1 = WriterType::New();
>   WriterType::Pointer writer2 = WriterType::New();
>   WriterType::Pointer writer3 = WriterType::New();
>   WriterType::Pointer writer4 = WriterType::New();
> 
>   caster1->SetInput( smoothing->GetOutput() );
>   writer1->SetInput( caster1->GetOutput() );
>   writer1->SetFileName("FastMarchingFilterOutput1.png");
>   caster1->SetOutputMinimum(   0 );
>   caster1->SetOutputMaximum( 255 );
>   writer1->Update();
> 
>   caster2->SetInput( gradientMagnitude->GetOutput() );
>   writer2->SetInput( caster2->GetOutput() );
>   writer2->SetFileName("FastMarchingFilterOutput2.png");
>   caster2->SetOutputMinimum(   0 );
>   caster2->SetOutputMaximum( 255 );
>   writer2->Update();
> 
>   caster3->SetInput( sigmoid->GetOutput() );
>   writer3->SetInput( caster3->GetOutput() );
>   writer3->SetFileName("FastMarchingFilterOutput3.png");
>   caster3->SetOutputMinimum(   0 );
>   caster3->SetOutputMaximum( 255 );
>   writer3->Update();
> 
>   caster4->SetInput( fastMarching->GetOutput() );
>   writer4->SetInput( caster4->GetOutput() );
>   writer4->SetFileName("FastMarchingFilterOutput4.png");
>   caster4->SetOutputMinimum(   0 );
>   caster4->SetOutputMaximum( 255 );
> 
>   // Software Guide : BeginCodeSnippet
>   fastMarching->SetOutputSize(
>            reader->GetOutput()->GetBufferedRegion().GetSize() );
>   // Software Guide : EndCodeSnippet
> 
> 
>   const double stoppingTime = atof( argv[9] );
> 
>   // Software Guide : BeginCodeSnippet
>   fastMarching->SetStoppingValue(  stoppingTime  );
>   // Software Guide : EndCodeSnippet
> 
> 
>   // Software Guide : BeginCodeSnippet
>   try
>     {
>     writer->Update();
>     }
>   catch( itk::ExceptionObject & excep )
>     {
>     std::cerr << "Exception caught !" << std::endl;
>     std::cerr << excep << std::endl;
>     }
>   // Software Guide : EndCodeSnippet
> 
> 
>   writer4->Update();
> 
>   typedef itk::ImageFileWriter< InternalImageType > InternalWriterType;
> 
>   InternalWriterType::Pointer mapWriter = InternalWriterType::New();
>   mapWriter->SetInput( fastMarching->GetOutput() );
>   mapWriter->SetFileName("FastMarchingFilterOutput4.mha");
>   mapWriter->Update();
> 
>   InternalWriterType::Pointer speedWriter = InternalWriterType::New();
>   speedWriter->SetInput( sigmoid->GetOutput() );
>   speedWriter->SetFileName("FastMarchingFilterOutput3.mha");
>   speedWriter->Update();
> 
>   InternalWriterType::Pointer gradientWriter = InternalWriterType::New();
>   gradientWriter->SetInput( gradientMagnitude->GetOutput() );
>   gradientWriter->SetFileName("FastMarchingFilterOutput2.mha");
>   gradientWriter->Update();
> 
> 
>   return 0;
> }
> 
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users


================================

-------------- next part --------------

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif



#include "itkCurvatureAnisotropicDiffusionImageFilter.h"
#include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
#include "itkSigmoidImageFilter.h"
#include "itkImage.h"
#include "itkFastMarchingImageFilter.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"


int main( int argc, char *argv[] )
{
  if( argc < 11 )
    {
    std::cerr << "Missing Parameters " << std::endl;
    std::cerr << "Usage: " << argv[0];
    std::cerr << " inputImage  outputImage seedX seedY";
    std::cerr << " Sigma SigmoidAlpha SigmoidBeta TimeThreshold StoppingValue seedZ" << std::endl;
    return 1;
    }

 
  typedef   float           InternalPixelType;
  const     unsigned int    Dimension = 3;
  typedef itk::Image< InternalPixelType, Dimension >  InternalImageType;
 

 
  typedef unsigned char OutputPixelType;
  typedef itk::Image< OutputPixelType, Dimension > OutputImageType;

  typedef itk::BinaryThresholdImageFilter< InternalImageType,
                        OutputImageType    >    ThresholdingFilterType;
  ThresholdingFilterType::Pointer thresholder = ThresholdingFilterType::New();

  const InternalPixelType  timeThreshold = atof( argv[8] );
 
  thresholder->SetLowerThreshold(           0.0  );
  thresholder->SetUpperThreshold( timeThreshold  );

  thresholder->SetOutsideValue(  0  );
  thresholder->SetInsideValue(  255 );

  typedef  itk::ImageFileReader< InternalImageType > ReaderType;
  typedef  itk::ImageFileWriter<  OutputImageType  > WriterType;

  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();

  reader->SetFileName( argv[1] );
  writer->SetFileName( argv[2] );

  typedef itk::RescaleIntensityImageFilter<
                               InternalImageType,
                               OutputImageType >   CastFilterType;

  typedef   itk::CurvatureAnisotropicDiffusionImageFilter<
                               InternalImageType,
                               InternalImageType>  SmoothingFilterType;
 
  SmoothingFilterType::Pointer smoothing = SmoothingFilterType::New();

  typedef   itk::GradientMagnitudeRecursiveGaussianImageFilter<
                               InternalImageType,
                               InternalImageType>  GradientFilterType;

  typedef   itk::SigmoidImageFilter<                              
                               InternalImageType,
                               InternalImageType>  SigmoidFilterType;

  GradientFilterType::Pointer  gradientMagnitude = GradientFilterType::New();
  SigmoidFilterType::Pointer sigmoid = SigmoidFilterType::New();

  sigmoid->SetOutputMinimum(  0.0  );
  sigmoid->SetOutputMaximum(  1.0  );

  typedef  itk::FastMarchingImageFilter< InternalImageType,
                              InternalImageType >    FastMarchingFilterType;

  FastMarchingFilterType::Pointer  fastMarching = FastMarchingFilterType::New();

  smoothing->SetInput( reader->GetOutput() );
  gradientMagnitude->SetInput( smoothing->GetOutput() );
  sigmoid->SetInput( gradientMagnitude->GetOutput() );
  fastMarching->SetInput( sigmoid->GetOutput() );
  thresholder->SetInput( fastMarching->GetOutput() );
  writer->SetInput( thresholder->GetOutput() );

  smoothing->SetTimeStep( 0.0625 );
  smoothing->SetNumberOfIterations(  1 );
  smoothing->SetConductanceParameter( 9.0 );


  const double sigma = atof( argv[5] );

  gradientMagnitude->SetSigma(  sigma  );


  const double alpha =  atof( argv[6] );
  const double beta  =  atof( argv[7] );

  sigmoid->SetAlpha( alpha );
  sigmoid->SetBeta(  beta  );

  typedef FastMarchingFilterType::NodeContainer           NodeContainer;
  typedef FastMarchingFilterType::NodeType                NodeType;
  NodeContainer::Pointer seeds = NodeContainer::New();
 

  InternalImageType::IndexType  seedPosition;
 
  seedPosition[0] = atoi( argv[3] );
  seedPosition[1] = atoi( argv[4] );
  seedPosition[2] = atoi( argv[10] );

  std::cout << "seedPosition " << seedPosition << std::endl;

  NodeType node;
  const double seedValue = 0.0;
 
  node.SetValue( seedValue );
  node.SetIndex( seedPosition );

  seeds->Initialize();
  seeds->InsertElement( 0, node );

  fastMarching->SetTrialPoints(  seeds  );

  CastFilterType::Pointer caster1 = CastFilterType::New();
  CastFilterType::Pointer caster2 = CastFilterType::New();
  CastFilterType::Pointer caster3 = CastFilterType::New();
  CastFilterType::Pointer caster4 = CastFilterType::New();

  WriterType::Pointer writer1 = WriterType::New();
  WriterType::Pointer writer2 = WriterType::New();
  WriterType::Pointer writer3 = WriterType::New();
  WriterType::Pointer writer4 = WriterType::New();

  caster1->SetInput( smoothing->GetOutput() );
  writer1->SetInput( caster1->GetOutput() );
  writer1->SetFileName("FastMarchingFilterOutput1.mhd");
  caster1->SetOutputMinimum(   0 );
  caster1->SetOutputMaximum( 255 );

  try
    {
    writer1->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }

  caster2->SetInput( gradientMagnitude->GetOutput() );
  writer2->SetInput( caster2->GetOutput() );
  writer2->SetFileName("FastMarchingFilterOutput2.mhd");
  caster2->SetOutputMinimum(   0 );
  caster2->SetOutputMaximum( 255 );

  try
    {
    writer2->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }



  caster3->SetInput( sigmoid->GetOutput() );
  writer3->SetInput( caster3->GetOutput() );
  writer3->SetFileName("FastMarchingFilterOutput3.mhd");
  caster3->SetOutputMinimum(   0 );
  caster3->SetOutputMaximum( 255 );

  try
    {
    writer3->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }



  caster4->SetInput( fastMarching->GetOutput() );
  writer4->SetInput( caster4->GetOutput() );
  writer4->SetFileName("FastMarchingFilterOutput4.mhd");
  caster4->SetOutputMinimum(   0 );
  caster4->SetOutputMaximum( 255 );

  // Software Guide : BeginCodeSnippet
  fastMarching->SetOutputSize(
           reader->GetOutput()->GetBufferedRegion().GetSize() );
  // Software Guide : EndCodeSnippet


  const double stoppingTime = atof( argv[9] );

  // Software Guide : BeginCodeSnippet
  fastMarching->SetStoppingValue(  stoppingTime  );
  // Software Guide : EndCodeSnippet


  // Software Guide : BeginCodeSnippet
  try
    {
    writer->Update();
    }
  catch( itk::ExceptionObject & excep )
    {
    std::cerr << "Exception caught !" << std::endl;
    std::cerr << excep << std::endl;
    }
  // Software Guide : EndCodeSnippet


  try
    {
    writer4->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }


  typedef itk::ImageFileWriter< InternalImageType > InternalWriterType;

  InternalWriterType::Pointer mapWriter = InternalWriterType::New();
  mapWriter->SetInput( fastMarching->GetOutput() );
  mapWriter->SetFileName("FastMarchingFilterOutput4.mha");
  
  try
    {
    mapWriter->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }


  InternalWriterType::Pointer speedWriter = InternalWriterType::New();
  speedWriter->SetInput( sigmoid->GetOutput() );
  speedWriter->SetFileName("FastMarchingFilterOutput3.mha");

  try
    {
    speedWriter->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }


  InternalWriterType::Pointer gradientWriter = InternalWriterType::New();
  gradientWriter->SetInput( gradientMagnitude->GetOutput() );
  gradientWriter->SetFileName("FastMarchingFilterOutput2.mha");

  try
    {
    gradientWriter->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }



  return 0;
}







More information about the Insight-users mailing list