[Insight-users] Setting the Spacing on a itkImageFileWriter object

Mike Jackson mike.jackson at imts.us
Wed Jan 31 13:55:33 EST 2007


I am using the example code reading in a series of images and then  
writing out a 3D vtk Image file. The problem I am having is that I  
need to change the spacing of the data. I have tried to change the  
spacing in the reader and the writer. I have tried instantiating my  
own ImageIO object and setting that into the itkImageFileWriter but  
the spacing is always set to 1,1,1.

I took a look down in the itkFileWriter.txx in the function write()  
and the code is copying the settings from the "input", which in my  
case should be an itkImageSeriesReader object. So any ideas on what I  
am doing wrong?

const unsigned int Dimension = 3;
typedef unsigned char                       PixelType;
typedef itk::Image< PixelType, Dimension >  ImageType;
typedef itk::ImageSeriesReader< ImageType >  ReaderType;
reader = ReaderType::New();
   float spacing[3] = {1.0f, 1.0f, 2.0f};
   reader->GetOutput()->SetSpacing(spacing);
  writer->SetFileName( outputFilename );
// We connect the output of the reader to the input of the writer.
   writer->SetInput( reader->GetOutput() );
// We enable compression.
   writer->SetUseCompression(true);
   itk::ImageIOBase::Pointer m_ImageIO;
   m_ImageIO = itk::ImageIOFactory::CreateImageIO( outputFilename,  
itk::ImageIOFactory::WriteMode );
   m_ImageIO->SetNumberOfDimensions(3);
   m_ImageIO->SetSpacing(0, 1.0);
   m_ImageIO->SetSpacing(1, 1.0);
   m_ImageIO->SetSpacing(2, 2.0);
   writer->SetImageIO(m_ImageIO);
   try {
     writer->Update();
   } catch( itk::ExceptionObject & err )  {
     std::cerr << "ExceptionObject caught !" << std::endl;
     std::cerr << err << std::endl;
     return false;
   }

Any help is appreciated.

This is with ITK 2.8.1 on OS X 10.4.8 (Intel) using gcc.
-- 
Mike Jackson   Senior Research Engineer
Innovative Management & Technology Services





More information about the Insight-users mailing list