-
Notifications
You must be signed in to change notification settings - Fork 5
/
resampling.py
39 lines (34 loc) · 1.54 KB
/
resampling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import SimpleITK as sitk
SITK_INTERPOLATOR_DICT = {
'nearest': sitk.sitkNearestNeighbor,
'linear': sitk.sitkLinear,
'gaussian': sitk.sitkGaussian,
'label_gaussian': sitk.sitkLabelGaussian,
'bspline': sitk.sitkBSpline,
'hamming_sinc': sitk.sitkHammingWindowedSinc,
'cosine_windowed_sinc': sitk.sitkCosineWindowedSinc,
'welch_windowed_sinc': sitk.sitkWelchWindowedSinc,
'lanczos_windowed_sinc': sitk.sitkLanczosWindowedSinc
}
def resample_image_to_spacing(image, new_spacing, default_value, interpolator='linear'):
assert interpolator in SITK_INTERPOLATOR_DICT, \
(f"Interpolator '{interpolator}' not part of SimpleITK. "
f"Please choose one of the following {list(SITK_INTERPOLATOR_DICT.keys())}.")
assert image.GetDimension() == len(new_spacing), \
(f"Input is {image.GetDimension()}-dimensional while "
f"the new spacing is {len(new_spacing)}-dimensional.")
interpolator = SITK_INTERPOLATOR_DICT[interpolator]
spacing = image.GetSpacing()
size = image.GetSize()
new_size = [int(round(siz * spac / n_spac)) for siz, spac, n_spac in zip(size, spacing, new_spacing)]
return sitk.Resample(
image,
new_size, # size
sitk.Transform(), # transform
interpolator, # interpolator
image.GetOrigin(), # outputOrigin
new_spacing, # outputSpacing
image.GetDirection(), # outputDirection
default_value, # defaultPixelValue
image.GetPixelID() # outputPixelType
)