flip two anatomical planes of the 3D image but failed

I want to mirror and flip two anatomical planes of the 3D image, using the following code. It was successful on the image size of (481481481), with two anatomical planes reversed, but failed on the other image size of (25625611), why is this, what should I do?

affineTrans = sitk.AffineTransform(3)
affineTrans.SetMatrix([-1,0,0,0,1,0,0,0,1])
affineTrans.SetCenter(CBCT_data.TransformContinuousIndexToPhysicalPoint([sz/2 for sz in CBCT_data.GetSize()]))
flipped = sitk.Resample(CBCT_data, affineTrans,sitk.sitkLinear,-1519)
image_viewer.Execute(flipped)

This matrix only flips along X axis. Also, I believe that resample will use identity matrix for the output image by default. If you want to directly compare it to the input, you should use input’s direction instead of identity.

Hello @ljjiayou,

As @dzenanz commented, this is a flip on the x axis, reflection about yz plane going through the middle of the volume.

If you are working with a human dataset and the anatomy of interest is kind of centered in the volume, at first glance it will appear that nothing has changed. This is because we are pretty symmetric with respect to the mid-sagittal plane (particularly if working with the head).

Look at the difference between the before and after image? sitk.Show(flipped - CBCT_data) to see that it did flip the image.

1 Like

I am not sure what the motivation is for this image permutation. But have you considered if the DICOMOrientImageFilter May do what you need?

1 Like

thanks

Using [DICOMOrientImageFilter] but itk-snap opens the image without any change,and flip doesn’t change. Why?
like this im=sitk.Flip(img,sitk.VectorBool([True,False,False]))

Hello @ljjiayou,

What is happening is that the organization of the image memory is modified as you wanted and the direction cosine matrix is also updated. This is saved to disk and ITK-SNAP knows that it should display the image using the correct anatomical orientation based on the direction cosine matrix. To the user, the two images will look the same even if the memory organization is different. If you use a viewer that doesn’t know how to do this you will see that the axes have indeed changed. Try it out with the following code snippet:

import SimpleITK as sitk

file_name = 
image = sitk.ReadImage(file_name)
image_axis_changed = sitk.DICOMOrient(image, "RAI")

sitk.Show(image, "original image")
sitk.Show(image_axis_changed, "axis changed")

sitk.WriteImage(image_axis_changed, "image_axis_changed.nrrd")
# open the saved nrrd image in ITK-SNAP and it will look the same as the original.
1 Like

thanks