Skip to content

Instantly share code, notes, and snippets.

@sshleifer
Last active May 5, 2019 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshleifer/a147a058564ea6cfb63c253c327129cc to your computer and use it in GitHub Desktop.
Save sshleifer/a147a058564ea6cfb63c253c327129cc to your computer and use it in GitHub Desktop.
import SimpleITK as sitk
import numpy as np
mask_file = '/data/ct-cspine/test_set_w_masks_2019_05_01/cspine_fx_seg/Cspine_fx_seg/5616571.nrrd'
array_file = '/data/ct-cspine/processed-studies/data_20180524_161757/anonymized_data/images/test/5616571.npy'
def projectImage(reference, moving, interpolate = 'linear'):
# projects moving image onto reference image space
# use  interpolate = 'NN' for segmentation masks
resample = sitk.ResampleImageFilter()
resample.SetReferenceImage(reference)
if interpolate == 'linear':
resample.SetInterpolator(sitk.sitkLinear)
elif interpolate == 'NN':
resample.SetInterpolator(sitk.sitkNearestNeighbor)
resampledimg = resample.Execute(moving)
return resampledimg
image = np.load(array_file)
mask_sitk = sitk.ReadImage(mask_file)
sitk_image = sitk.GetImageFromArray(image) # also fails if you pass image.T
#You can do:
#nearest neighbor interpolation for 1 or 0 masks
alignedmask = projectImage(sitk_image, mask_sitk, interpolate = 'NN')
alignedmasknpy = sitk.GetArrayFromImage(alignedmask).T
mask_arr = sitk.GetArrayFromImage(mask_sitk)
imgnpy = sitk.GetArrayFromImage(sitk_image).T
print(alignedmasknpy.max())
print(mask_arr.max())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment