2

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)
BlackSpecter
  • 61
  • 2
  • 12

1 Answers1

4

The new voxel spacing will determine the new image dimensions.

spacing = itk_ct_scan.GetSpacing()
size = itk_image.GetSize()
new_spacing = [1.5,1.5,1.5]
new_size = (np.round(size*(spacing/np.array(new_spacing)))).astype(int).tolist()

You can resize the image to required voxel spacing by resampling

resampled_img = sitk.Resample(itk_image, new_size, sitk.Transform(),
sitk.sitkNearestNeighbor, itk_image.GetOrigin(), new_spacing,
                              itk_image.GetDirection(), 0.0, itk_image.GetPixelID())

However, in your case since you want the new image of required size you can use

resampled_img = sitk.Resample(itk_image, [128, 128, 128], sitk.Transform(),
sitk.sitkNearestNeighbor, itk_image.GetOrigin(), new_spacing,
                              itk_image.GetDirection(), 0.0, itk_image.GetPixelID())
mujjiga
  • 16,186
  • 2
  • 33
  • 51
  • But this will make the new spacing as [1.875, 1.875, 1.2175]? I need some sort of isotropic sampling at the size of (128, 128, 128). – BlackSpecter Mar 01 '19 at 20:06
  • resampled_img = sitk.Resample(itk_image, [128, 128, 128], sitk.Transform(), sitk.sitkNearestNeighbor, itk_image.GetOrigin(), [1,1,1], itk_image.GetDirection(), 0.0, itk_image.GetPixelID()) will return resampled image of [128,128,128] size and [1,1,1] spacing and uses NearestNeighbour for interpolation. – mujjiga Mar 04 '19 at 11:52