[Insight-users] GradientVectorFlowImageFilter: file writting + fix !

Mathieu Malaterre malat@free.fr
Wed, 12 Feb 2003 13:43:34 +0100


Hi all,

   While I was playing around with GradientVectorFlowImageFilter I tried 
to save the output to a file. But currently ITK can't handle VTK file 
with 3 components...so here is the fix + example file:

//The fix
// go to line 230 of file Insight/Code/IO/itkImageIOBase.cxx
//and change this way:
  else if ( ptype == typeid(CovariantVector<float,2>) )
    {
    this->SetNumberOfComponents(2);
    this->SetPixelType(ImageIOBase::FLOAT);
    this->SetComponentType(ImageIOBase::FLOAT);
    }
  else if ( ptype == typeid(CovariantVector<float,3>) )
    {
    this->SetNumberOfComponents(3);
    this->SetPixelType(ImageIOBase::FLOAT);
    this->SetComponentType(ImageIOBase::FLOAT);
    }
  else
    {
    itkExceptionMacro("Pixel type currently not supported.");
    this->SetPixelType(ImageIOBase::UNKNOWN);
    this->SetComponentType(ImageIOBase::UNKNOWN);
    return false;
    }
//end of fix



Example program (could be placed in Insight/Example/IO):



//Beginning of file: ImageReadWriteVTK.cxx

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"
#include "itkVTKImageIO.h"


int main( int argc, char ** argv )
{

  // Verify the number of parameters in the command line
  if( argc < 3 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " inputImageFile  outputImageFile " << 
std::endl;
    return -1;
    }

  typedef float            PixelType;
  const   unsigned int        Dimension = 3;

  typedef itk::CovariantVector<PixelType, Dimension> GradientPixelType;
  typedef itk::Image<GradientPixelType, Dimension>    GradientImageType;

  typedef itk::ImageFileReader< GradientImageType >  ReaderType;
  typedef itk::ImageFileWriter< GradientImageType >  WriterType;


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


  const char * inputFilename  = argv[1];
  const char * outputFilename = argv[2];

  itk::VTKImageIO::Pointer vtkIO = itk::VTKImageIO::New();
  vtkIO->SetNumberOfComponents(3);

  reader->SetFileName( inputFilename  );
  reader->SetImageIO(vtkIO);
 
  writer->SetFileName( outputFilename );
  writer->SetInput( reader->GetOutput() );
  writer->SetImageIO(vtkIO);

  try
    {
    writer->Update();
    }
  catch( itk::ExceptionObject & err )
    {
    std::cout << "ExceptionObject caught !" << std::endl;
    std::cout << err << std::endl;
    return -1;
    }

  return 0;


}

//End of file: ImageReadWriteVTK.cxx