Inappropriate output for rotating a volume using Euler transform in C++

The below code takes the axial volume as input, applies a 90-degree rotation around the z-axis, and writes the output volume to a 90.nrrd file. In 90. When viewing the NRRD file in the ITK-SNAP app, we can see that certain pixels are black in both coronal and sagittal views. You can see this missing area in the attached output image where I have highlighted those regions using red boxes.

My C++ code:

typedef itk::Image<signed short, 3> ImageType; 
itk::Euler3DTransform<double>::Pointer transform = itk::Euler3DTransform<double>::New(); 
itk::ResampleImageFilter<ImageType, ImageType>::Pointer spinFilter = itk::ResampleImageFilter<ImageType3D, ImageType3D>::New();

auto spacing = image->GetSpacing();
auto origin = image->GetOrigin();
auto imageSize = image->GetLargestPossibleRegion().GetSize();
itk::Index<3> index{ imageSize[0] / 2, imageSize[1] / 2, imageSize[2] / 2 };
itk::Point<double, 3> center; image->TransformIndexToPhysicalPoint(index, center);

spinFilter->SetDefaultPixelValue(-32767);
spinFilter->SetOutputOrigin(origin);
spinFilter->SetSize(imageSize);
spinFilter->SetOutputSpacing(spacing);
spinFilter->SetOutputDirection(image->GetDirection());
transform->SetCenter(center);
transform->SetRotation(thetaX, thetaY, thetaZ);
spinFilter->SetTransform(transform);
spinFilter->SetInput(image);
itk::ImageFileWriter<ImageType>::Pointer writer = itk::ImageFileWriter<ImageType3D>::New();
writer->SetFileName("D:/outs/90.nrrd");
writer->SetInput(spinFilter->GetOutput());
writer->Update();

Code OUTPUT:

But when, instead of rotating using C++ code, if I rotate my axial volume in the ITK-SNAP app, the image is proper, as we can see in the image below.

ITK-SNAP Rotate output:

What might be the issue here.

Instead of itk::ResampleImageFilter I have also tried using itk::TransformGeometryImageFilter.

While using TransformGeometry filter, It works fine for 0, 90, 180, and 270 degrees, but fails for all other intermediary angles

How does it fail? ITK-SNAP displays slices in an image-axis-aligned manner, so e.g. 5 or 30 degree rotation would not be displayed differently. Have you tried visualizing your images in 3D Slicer? While Slicer displays images in a grid which is image-aligned by default (at least in latest stable version, 5.8.1), you can easily switch that to world-aligned:

Hello @purushothaman_u,

There is a logical bug in the code, the definition of the grid to use for resampling is incorrect. This grid is defined by the paramters to the resample filter: origin, size, spacing and direction. In the code these are the original image’s values. Once that image is rotated portions of it will fall outside the original grid which is likely the reason for what you are seeing.

To resolve this you will need to compute new values for these parameters. Apply the inverse transformation to the vertices (physical space) of the original volume’s bounding box. Use the original image spacing and compute the new origin (minimum on all three axes of the transformed vertices) and size (maximum on all three axes minus new origin divided by spacing). Set the direction to identity.

3 Likes

@dzenanz
When using 3d slicer I can see my volume being rotated. Thanks for your input, Its working fine

1 Like

@zivy

I’ll correct this, Thanks for your input