Are you mixing and matching SimpleITK and nibabel? Possibly that is the difference:
import SimpleITK as sitk
import nibabel as nb
# create image with sizes x=10, y=20, z=30 and write to disk
file_name = "1.nii"
image = sitk.Image([10,20,30], sitk.sitkUInt16)
sitk.WriteImage(image, file_name)
# get numpy arrays using nibabel's load and SimpleITK read+get_array
nb_arr = nb.load(file_name)
sitk_arr = sitk.GetArrayFromImage(sitk.ReadImage(file_name))
print(nb_arr.shape)
print(sitk_arr.shape)
We are not mixing SimpleITK and Nibabel. We are only using SimpleITK. The image detail and mask detail are shown here. When we visualize the image and the mask without transpose, there is a mismatch. After doing transposition it shows correct superposition. It means in each slice x and y dimension should be swapped to get the right image with the right mask.
That’s what we did after so many trials and errors and now it is working fine. Basically, we included a for loop to iterate over each slice and within which we swapped the x and y dimension. Now without transpose it is showing correct superposition. Overall dimension has not changed that is 256, 256, 216.