Resolving Image Cropping Issue in NIFTI Image Resampling

I am trying to resample a NIFTI image from a resolution of 512,512,661 to 512,512,147 using SimpleITK. Below is the code I’ve used:

def resample_to_original_size(isotropic_img, ref_img):
    original_spacing = ref_img.GetSpacing()
    original_size = ref_img.GetSize()
    
    resampler = sitk.ResampleImageFilter()
    resampler.SetInterpolator(sitk.sitkLinear)  
    resampler.SetOutputSpacing(original_spacing)
    resampler.SetSize(original_size)
    resampler.SetOutputOrigin(ref_img.GetOrigin())
    resampler.SetOutputDirection(ref_img.GetDirection())
    
    resampled_img = resampler.Execute(isotropic_img)

    return resampled_img

In this situation, ‘isotropic_img’ has a resolution of 512,512,661 and the spacing is set to 1 for all dimensions. However, when I check the resampled_img, it appears as if the images have been cropped. How can this issue be resolved?

You want the 147 Z-slices of the new image to span the same space as the 661 Z-slices of the isotopic image. So, assuming the isotropic image has a Z-spacing of 1, the new image should have a Z-spacing of 661/147.

In the SimpleITK Utilities we have a resize function that might do what you intend:

http://simpleitk.org/SimpleITKUtilities/api.html#SimpleITK.utilities.resize

Thank you for your kind response.
However, I believe I omitted a crucial detail in my question. I would like to resample an image from a size of (512,512,612) and spacing of (1,1,1) to a size of (512,512,147) and spacing of (0.668,0.668,3). In other words, I want to change the spacing as well. Is this possible?

That doesn’t really work out. Your original image spans 512mm, 512mm, and 612mm, assuming your units are 1mm. For your new volume, with size (512,512,147) and spacing (.668, .668, 3), that works out to (342.0, 342.0, 441.0) in millimeters. So you’re definitely going to end up with a cropped sub-volume.

If you want to span the space of (512,512,612) millimeters with spacing of (.668, .668, 3) millimeters, then the volume dimensions would have to be (767, 767, 204).

And that assumes that you use the original origin and direction matrix.

1 Like