Skip to content

Instantly share code, notes, and snippets.

@afg1
Created December 3, 2018 16:07
Show Gist options
  • Save afg1/4b5e8b0d99ba2ca6b6ec1c66f54c7856 to your computer and use it in GitHub Desktop.
Save afg1/4b5e8b0d99ba2ca6b6ec1c66f54c7856 to your computer and use it in GitHub Desktop.
Example of resampling to a coarser image grid using SimpleITK
import SimpleITK as sitk
import numpy as np
## Just something I had lying around
inputImage = "ct.nii"
## Load the image. Must be a simpleITK image for this stage to work!
startImage = sitk.ReadImage(inputImage)
## factors to downsize by, this will take a 512x512x118 image to 128x128x118
factors = [4, 4, 1]
## NB, if you wanted to resample to a uniform number of slices, you could figure out what the 1 at the end should be
## Calculate the new pixel spacing and image size
new_spacing = [a*b for a,b in zip(startImage.GetSpacing(), factors)]
new_size = [int(a/b) for a,b in zip(startImage.GetSize(),factors)]
## do some in-plane blurring
sig = 2.0/(2.0*np.sqrt(np.pi)) ## this comes from the Nyquist-Shannon theorem: https://en.wikipedia.org/wiki/Nyquist–Shannon_sampling_theorem
## Blur in each plane separately (NB; there may be a more elegant way to do this, but I didn't try)
blurredImage = sitk.RecursiveGaussian(startImage, sigma=sig*new_spacing[0], direction=0)
blurredImage = sitk.RecursiveGaussian(blurredImage, sigma=sig*new_spacing[1], direction=1)
## Build the resampling filter, and then apply it
resampleFilter = sitk.ResampleImageFilter()
downsampledImage = resampleFilter.Execute(blurredImage,## thing to resample
new_size, ## size to resample to
sitk.Transform(), ## How to transform - in this case, identity since we're just resampling on a coarser grid
sitk.sitkBSpline, ## could also do sitk.sitkLinear which is faster, but worse
startImage.GetOrigin(), ## Keep the same origin
new_spacing, ## The new pixel size
startImage.GetDirection(), ## Keep the same slice direction
0, ## Default pixel value
startImage.GetPixelIDValue()) ## output pixel type
## Write the result to check
sitk.WriteImage(downsampledImage, "test_dsCT.nii")
## At this point you could also get the numpy array and go do whatever you like with it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment