My current image is of size (240, 240, 155) with a voxel spacing of (1, 1, 1). My final image should be of (128, 128, 128) with a voxel spacing of (1.5, 1.5, 1.5). Is there a way to understand this phenomenon of resizing with voxel spacing? I have tried the following but it wasn't satisfactory enough for me.
import SimpleITK as sitk
import numpy as np
from skimage.transform import resize
def resize_image(image, old_spacing, new_spacing, order=3):
new_shape = (int(np.round(old_spacing[0]/new_spacing[0]*float(image.shape[0]))),
int(np.round(old_spacing[1]/new_spacing[1]*float(image.shape[1]))),
int(np.round(old_spacing[2]/new_spacing[2]*float(image.shape[2]))))
return resize(image, new_shape, order=order, mode='edge', cval=0, anti_aliasing=False)
file_path = 'some_file'
itk_image = sitk.ReadImage(file_path)
spacing = np.array(itk_image.GetSpacing())[[2, 1, 0]]
spacing_target = (1.5, 1.5, 1.5)
image = sitk.GetArrayFromImage(itk_image).astype(float)
if np.any([[i != j] for i, j in zip(spacing, spacing_target)]):
new_image = resize_image(image, spacing, spacing_target).astype(np.float32)