def resample_isocubic(img, new_spacing):
    def calc_new_size():
        np_size = np.array(img.GetSize())
        np_cur_spacing = np.array(img.GetSpacing())
        np_new_spacing = np.array(new_spacing)
        
        np_new_size = np_size * np_cur_spacing / new_spacing
        return np_new_size.astype(int).tolist()
        
    origin = img.GetOrigin()
    size = img.GetSize()
    cur_spacing = img.GetSpacing()
    direction = img.GetDirection()
    
    new_size = calc_new_size()
    print(new_size)
    transform = sitk.Transform()                # default is 'IdentityTransform'
    interpolator = sitk.sitkNearestNeighbor
    
    return sitk.Resample(img, 
                         new_size,
                         transform,
                         interpolator,
                         origin,
                         new_spacing,
                         direction,
                         0.0,
                         img.GetPixelIDValue())
def resampleMask_isocubic(img, base_spacing, ref_img):
    img.SetSpacing(base_spacing)                # change with the same spacing of reference
    transform = sitk.Transform()                # default is 'IdentityTransform'
    interpolator = sitk.sitkNearestNeighbor
    
    return sitk.Resample(img, 
                         ref_img.GetSize(),
                         transform,
                         interpolator,
                         ref_img.GetOrigin(),
                         ref_img.GetSpacing(),
                         ref_img.GetDirection(),
                         0.0,
                         img.GetPixelIDValue())
def resampleMask_isocubic(img, base_spacing, ref_img):
    img.SetSpacing(base_spacing)                # change with the same spacing of reference
    transform = sitk.Transform()                # default is 'IdentityTransform'
    interpolator = sitk.sitkNearestNeighbor
    
    return sitk.Resample(img, 
                         ref_img.GetSize(),
                         transform,
                         interpolator,
                         ref_img.GetOrigin(),
                         ref_img.GetSpacing(),
                         ref_img.GetDirection(),
                         0.0,
                         img.GetPixelIDValue())
input_img = read_cardiac_image(task='CHD', is_label=False, file='sample_dia.mha')
show_info(input_img)
show.myshow(input_img)
base_spacing = input_img.GetSpacing()
isocubic_img = resample_isocubic(input_img, (0.5, 0.5, 0.5))
show_info(isocubic_img)
show.myshow(isocubic_img)

here is a full sample code

"""
isotropically reslicing example code
"""

import SimpleITK as sitk , cv2, numpy as np , matplotlib.pyplot as plt , os, shutil

def resample_isocubic(img, new_spacing):
    def calc_new_size():
        np_size = np.array(img.GetSize())
        np_cur_spacing = np.array(img.GetSpacing())
        np_new_spacing = np.array(new_spacing)
        
        np_new_size = np_size * np_cur_spacing / new_spacing
        return np_new_size.astype(int).tolist()
        
    origin = img.GetOrigin()
    size = img.GetSize()
    cur_spacing = img.GetSpacing()
    direction = img.GetDirection()
    
    new_size = calc_new_size()
    print(new_size)
    transform = sitk.Transform()                # default is 'IdentityTransform'
    interpolator = sitk.sitkNearestNeighbor
    
    return sitk.Resample(img, 
                         new_size,
                         transform,
                         interpolator,
                         origin,
                         new_spacing,
                         direction,
                         0.0,
                         img.GetPixelIDValue())

def resampleMask_isocubic(img, base_spacing, ref_img):
    img.SetSpacing(base_spacing)                # change with the same spacing of reference
    transform = sitk.Transform()                # default is 'IdentityTransform'
    interpolator = sitk.sitkNearestNeighbor
    
    return sitk.Resample(img, 
                         ref_img.GetSize(),
                         transform,
                         interpolator,
                         ref_img.GetOrigin(),
                         ref_img.GetSpacing(),
                         ref_img.GetDirection(),
                         0.0,
                         img.GetPixelIDValue())

# mha_file = "../chd_sample/001_dia_M.mha"
mha_file = "../chd_sample/001_dia.mha"
mask_mha_file = "../chd_sample/001_dia_M.mha"


image = sitk.ReadImage(mha_file)
maskimage = sitk.ReadImage(mask_mha_file)

raw_spacing = image.GetSpacing()
direction = image.GetDirection()
size = image.GetSize()

print("spacing={}  , direction={}, size={}".format(raw_spacing, direction, size))

new_spacing = (0.5, 0.5, 0.5)
resampled_image = resample_isocubic(image, new_spacing)


spacing = resampled_image.GetSpacing()
direction = resampled_image.GetDirection()
size = resampled_image.GetSize()

print("after resampling >> spacing={}  , direction={}, size={}".format(spacing, direction, size))


resampled_mask_image = resampleMask_isocubic(maskimage, raw_spacing, resampled_image)

spacing = resampled_mask_image.GetSpacing()
direction = resampled_mask_image.GetDirection()
size = resampled_mask_image.GetSize()

print("after resampling mask image >> spacing={}  , direction={}, size={}".format(spacing, direction, size))


# array = sitk.GetArrayFromImage(image) # shape: 171 , 512,512
Categories: python

0 Comments

Leave a Reply

Your email address will not be published.