[ITK-users] SetOutputOrigin(origin) not working on writer

Matias Montroull matimontg at gmail.com
Sat May 14 19:42:44 EDT 2016


Hi,

I have the following code to flip an image and preserve origin and spacing.
For some reason, the writer doesn't copy the Origin from the original image
into the output Image.

I can see the Output Image origin is the Original Image origin but I don't
understand why the writer doesn't take that value. I have no issues with
the spacing, that one is copied just fine into the output image.

I'm using ITK 4.7.2

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkFlipImageFilter.h"
#include "itkChangeInformationImageFilter.h"

int main(int argc, char* argv[])
{
if (argc != 4)
{
std::cerr << "Usage: " << argv[0];
std::cerr << " <InputFileName> <OutputFileName> <AxisToFlip>";
std::cerr << std::endl;
return EXIT_FAILURE;
}

typedef itk::Image<signed short, 2 >  ImageType;

typedef itk::ImageFileReader< ImageType >   ReaderType;
ReaderType::Pointer reader = ReaderType::New();

reader->SetFileName(argv[1]);

ImageType::Pointer inputImage = reader->GetOutput();
    inputImage->Update(); //Importante!! Sin esto no agarra el origen

ImageType::PointType     origin = inputImage->GetOrigin();
ImageType::SpacingType   spacing = inputImage->GetSpacing();

typedef itk::FlipImageFilter< ImageType >   FlipImageFilterType;
FlipImageFilterType::Pointer flipFilter = FlipImageFilterType::New();

typedef itk::ChangeInformationImageFilter< ImageType >  FilterType;
    FilterType::Pointer filter = FilterType::New();
filter->SetInput(reader->GetOutput());

filter->SetOutputOrigin(origin);
filter->ChangeOriginOn();
filter->SetOutputSpacing(spacing);
filter->ChangeSpacingOn();

flipFilter->SetInput(filter->GetOutput());
flipFilter->Update();

FlipImageFilterType::FlipAxesArrayType flipAxes;
if (atoi(argv[3]) == 0)
{
flipAxes[0] = true;
flipAxes[1] = false;
}
else
{
flipAxes[0] = false;
flipAxes[1] = true;
}

flipFilter->SetFlipAxes(flipAxes);
typedef itk::ImageFileWriter< ImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(argv[2]);
writer->SetInput(flipFilter->GetOutput());
try
{
writer->Update();
std::cout << "Input origin is: " << reader->GetOutput()->GetOrigin() <<
std::endl;
std::cout << "Output origin is: " << flipFilter->GetOutput()->GetOrigin()
<< std::endl;
}
catch (itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

-- 
Matias
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20160514/4560f55c/attachment.html>


More information about the Insight-users mailing list