How to reshape 4D .nii image

I have a .nii image of size 30, 28, 256, 256. I want to reshape it to 256, 256, 28, 30. I tried converting to numpy array and then using sitk.GetImageFromArray(). However, the size of the resulting image is 256, 256, 28. The fourth dimension is lost.

How can I get the desired shape of the image?

I believe the PermuteImageFilter can do what you like:
https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1PermuteAxesImageFilter.html

Also keep in mind that the index order of a numpy array and a SimpleITK image are opposite, so mixing the two with out clear boundaries ( like functions ) is error prone.

1 Like

Please tell me the python code to call PermuteAxesImageFilter.
I wrote the following lines,

image=sitk.ReadImage("Moving.nii",sitk.sitkFloat32)
sitk.sitkPermuteAxesImageFilter.SetOrder(image, [256, 256, 28, 30])

It showed the following error,

AttributeError                            Traceback (most recent call last)
Cell In[3], line 2
      1 image=sitk.ReadImage("Moving.nii",sitk.sitkFloat32)
----> 2 sitk.sitkPermuteAxesImageFilter.SetOrder(image, [256, 256, 28, 30])

AttributeError: module 'SimpleITK' has no attribute 'sitkPermuteAxesImageFilter'

Hello @debapriya,

Please read the documentation @blowekamp pointed to. That is a class, you create an instance and call the class methods as you do in regular Python programming.

1 Like

The order is the axis index [0-4], not the axis size. Something like the following should work:

import SimpleITK as sitk
img = sitk.Image([30,28,256,256], sitk.sitkFloat32)
sitk.WriteImage(img, "test.nii")
image = sitk.ReadImage("test.nii")
print(f"{image.GetSize()=}")
out = sitk.PermuteAxes(image, order=[3,2,1,0])
print(f"{out.GetSize()=}")
3 Likes