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

Luis Ibanez luis.ibanez at kitware.com
Sun Feb 4 13:43:55 EST 2007


Hi Mike,

The ImageFileWriter is not the appropriate place
for changing the spacing of an image.

Instead,
you should use the ChangeImageInformationFilter.

http://www.itk.org/Insight/Doxygen/html/classitk_1_1ChangeInformationImageFilter.html

and connect it *BEFORE* the writer.


Something like:


    typedef itk::ChangeInformationImageFilter< ImageType > ChangerType;
    ChangerType::Pointer changer = ChangerType::New();

    changer->SetInput( reader->GetOutput() );

    ImageType::SpacingType spacing;

    spacing[0] = 2.7;
    spacing[1] = 1.3;  // etc...

    changer->SetOutputSpacing( spacing );

    writer->SetInput( changer->GetOutput() );




   Regards,


      Luis


=======================
Mike Jackson wrote:
> 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.


More information about the Insight-users mailing list