Resampling to volume to smaller size and smaller voxel spacing

Hi @zivy

I realized something about my image only now, and now my goals have changed. Now I want to keep my PET images as it is, and only resample CT images to PET size and spacing. But the origin of PET and their corresponding CT images are not the same, and the Direction cosines may also need not be the same. The physical size of the CT image is also slightly smaller than the corresponding PET image. But in the current image pairs, the PET and CT images are well aligned/registered. I am using the following code to resample CT to PET size and spacing now:

import SimpleITK as sitk

def resample_ct2pt(ctimg, ptimg, interpolator):#, interpolator, new_size, new_spacing):
    resampler = sitk.ResampleImageFilter()
    resampler.SetOutputOrigin(ctimg.GetOrigin())
    resampler.SetOutputDirection(ctimg.GetDirection())
    resampler.SetSize(ptimg.GetSize())
    resampler.SetOutputSpacing(ptimg.GetSpacing())
    resampler.SetInterpolator(interpolator)
    resampled_ctimg = resampler.Execute(ctimg)
    
    return resampled_ctimg 

ctpath = # ct image here
ptpath = # pet image here
info = os.path.basename(ptpath).split('_')
filename = info[0] + '_' + info[1]

ctimg = sitk.ReadImage(ctpath, imageIO="NiftiImageIO")
ptimg = sitk.ReadImage(ptpath, imageIO="NiftiImageIO")

resampled_ctimg = resample_ct2pt(ctimg, ptimg, sitk.sitkLinear)
sitk.WriteImage(resampled_ctimg,filename+'_ct_new.nii')

I have the following two screenshots: The top image is the PET/CT overlapped in the Lifex software before resampling, and the bottom image is after resampling. I have a feeling that they might not be as well aligned as before (because of the new grey region that appears on the right side of the image in the bottom images). How can I ensure they are aligned in the same way after resampling as they were before?

Thank you for your help so far.