Hi ITK community,
I have recently started learning SimpleITK and have been stuck on a specific type of Resampling that I have been trying to implement. I have PET/CT Nifti images. My typical PET and CT volumes have the following characteristics:
PET Size: (168, 168, 324)
PET Spacing: (4.07283, 4.07283, 3.0)
CT Size: (512, 512, 486)
CT Spacing: (0.9765625, 0.9765625, 2.0)
I want to downsample my CT images to match their corresponding PET Size, but I also want all my voxel spacing for both PET and CT to be isotropic and equal to (1.0, 1.0, 1.0). So far, I have tried codes by people who have already asked similar questions, but most of them ended up cropping my final image. Is it possible to perform this resampling?
I am attaching the latest version of my code where I am trying to resample a PET image to a volume where I keep the size the same as the original PET image, but change the voxel spacing to (1.0, 1.0, 1.0). This code results in getting the final resampled image cropped. Please let me know what I am doing wrong.
Thanks in advance for your help.
def reSample_fixedSize_fixedSpacing (img_sitk,interpolation, new_spacing):
dimension = img_sitk.GetDimension()
reference_size = img_sitk.GetSize()#[new_size,new_size,new_size]
reference_physical_size = np.zeros(img_sitk.GetDimension())
reference_physical_size[:] = [(sz-1)*spc if sz*spc>mx else mx for sz,spc,mx in zip(img_sitk.GetSize(), img_sitk.GetSpacing(), reference_physical_size)]
reference_direction = img_sitk.GetDirection()
reference_origin = img_sitk.GetOrigin()
reference_image = sitk.Image(reference_size, img_sitk.GetPixelIDValue())
reference_image.SetOrigin(reference_origin)
reference_image.SetSpacing(new_spacing)
reference_image.SetDirection(reference_direction)
reference_center = np.array(reference_image.TransformContinuousIndexToPhysicalPoint(np.array(reference_image.GetSize())/2.0))
transform = sitk.AffineTransform(dimension)
transform.SetMatrix(img_sitk.GetDirection())
transform.SetTranslation(np.array(img_sitk.GetOrigin()) - reference_origin)
centering_transform = sitk.TranslationTransform(dimension)
img_center = np.array(img_sitk.TransformContinuousIndexToPhysicalPoint(np.array(img_sitk.GetSize())/2.0))
centering_transform.SetOffset(np.array(transform.GetInverse().TransformPoint(img_center) - reference_center))
centered_transform = sitk.CompositeTransform([centering_transform,transform])
new_img = sitk.Resample(img_sitk, reference_image, centered_transform, interpolation, 0.0)
return new_img
ptpath = '14695318_20170802_pt.nii.gz'
ptimg = sitk.ReadImage(ptpath, imageIO="NiftiImageIO")
resamp_ptimg = reSample_fixedSize_fixedSpacing(ptimg, sitk.sitkBSpline, [1.0, 1.0, 1.0])
info = os.path.basename(ptpath).split('_')
filename = info[0] + '_' + info[1]
sitk.WriteImage(resamp_ptimg,filename+'_pt_resamp.nii')