Edited: typo
FYI, my test, original image, (BTW it is signed short image, but opened as float to reproduce above test:
r@deb:~/tmp/test038/build$ ./test038 original.nii.gz
original LPI [86.9175, 90.0284, -101.794]
filter output LPI->RAI [-100.04, -158.669, -101.794]
filter output RAI->LPI [86.9175, 90.0284, -101.794]
re-opened LPI->RAI [-100.04, -158.669, -101.794]
re-opened RAI->LPI [86.9175, 90.0284, -101.794]
Here is code
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkSpatialOrientation.h"
#include "itkOrientImageFilter.h"
template<typename T> bool orient_filter(
const typename T::Pointer & image,
typename T::Pointer & out_image,
int f)
{
if (image.IsNull()) return false;
typedef itk::OrientImageFilter<T,T> OrientImageFilterType;
typename OrientImageFilterType::Pointer filter = OrientImageFilterType::New();
try
{
filter->SetInput(image);
filter->UseImageDirectionOn();
filter->SetDesiredCoordinateOrientation(
static_cast<itk::SpatialOrientation::ValidCoordinateOrientationFlags>(f));
filter->Update();
out_image = filter->GetOutput();
}
catch (itk::ExceptionObject & ex)
{
std::cout << ex.GetDescription() << std::endl;
return false;
}
if (out_image.IsNotNull()) out_image->DisconnectPipeline();
else return false;
return true;
}
int main(int argc, char ** argv)
{
typedef itk::Image<float, 3> ImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;
typedef itk::ImageFileWriter<ImageType> WriterType;
ImageType::Pointer inputLPI;
ImageType::Pointer outputRAI;
ImageType::Pointer outputLPI;
ImageType::Pointer reopenedRAI;
ImageType::Pointer reopenedLPI;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
try
{
reader->Update();
inputLPI = reader->GetOutput();
}
catch (itk::ExceptionObject & ex)
{
std::cout << ex.GetDescription() << std::endl;
return 1;
}
std::cout << "original LPI " << inputLPI->GetOrigin()
<< std::endl;
orient_filter<ImageType>(
inputLPI,
outputRAI,
(int)itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_RAI);
std::cout << "filter output LPI->RAI " << outputRAI->GetOrigin()
<< std::endl;
orient_filter<ImageType>(
outputRAI,
outputLPI,
(int)itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_LPI);
std::cout << "filter output RAI->LPI " << outputLPI->GetOrigin()
<< std::endl;
WriterType::Pointer writer1 = WriterType::New();
WriterType::Pointer writer2 = WriterType::New();
writer1->SetInput(outputRAI);
writer1->SetFileName("outputRAI.nii.gz");
writer2->SetInput(outputLPI);
writer2->SetFileName("outputLPI.nii.gz");
try
{
writer1->Update();
writer2->Update();
}
catch (itk::ExceptionObject & ex)
{
std::cout << ex.GetDescription() << std::endl;
return 1;
}
ReaderType::Pointer reader1 = ReaderType::New();
ReaderType::Pointer reader2 = ReaderType::New();
reader1->SetFileName("outputRAI.nii.gz");
reader2->SetFileName("outputLPI.nii.gz");
try
{
reader1->Update();
reader2->Update();
reopenedRAI = reader1->GetOutput();
reopenedLPI = reader2->GetOutput();
}
catch (itk::ExceptionObject & ex)
{
std::cout << ex.GetDescription() << std::endl;
return 1;
}
std::cout << "re-opened LPI->RAI " << reopenedRAI->GetOrigin()
<< std::endl;
std::cout << "re-opened RAI->LPI n" << reopenedLPI->GetOrigin()
<< std::endl;
return 0;
}
nnn