Skip to content

Commit

Permalink
Allow setting resampling interpolator
Browse files Browse the repository at this point in the history
  • Loading branch information
joeranbosma committed Nov 27, 2024
1 parent bbf361d commit 87a0e39
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
long_description = fh.read()

setuptools.setup(
version='2.1.8',
version='2.1.9',
author_email='Joeran.Bosma@radboudumc.nl',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
17 changes: 12 additions & 5 deletions src/picai_prep/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ class PreprocessingSettings():
- physical_size: size in mm of the target image (z, y, x)
- crop_only: only crop to specified size (i.e., do not pad)
- align_segmentation: whether to align the scans using the centroid of the provided segmentation
- scan_interpolator: interpolation method for scans
- lbl_interpolator: interpolation method for labels
"""
matrix_size: Optional[Iterable[int]] = None
spacing: Optional[Iterable[float]] = None
physical_size: Optional[Iterable[float]] = None
crop_only: bool = False
align_segmentation: Optional[sitk.Image] = None
scan_interpolator = sitk.sitkBSpline
lbl_interpolator = sitk.sitkNearestNeighbor

def __post_init__(self):
if self.physical_size is None and self.spacing is not None and self.matrix_size is not None:
Expand Down Expand Up @@ -73,6 +77,7 @@ def resample_img(
out_spacing: Iterable[float] = (2.0, 2.0, 2.0),
out_size: Optional[Iterable[int]] = None,
is_label: bool = False,
interpolation = None,
pad_value: Optional[Union[float, int]] = 0.,
) -> sitk.Image:
"""
Expand Down Expand Up @@ -107,7 +112,9 @@ def resample_img(
resample.SetOutputOrigin(image.GetOrigin())
resample.SetTransform(sitk.Transform())
resample.SetDefaultPixelValue(pad_value)
if is_label:
if interpolation is not None:
resample.SetInterpolator(interpolation)
elif is_label:
resample.SetInterpolator(sitk.sitkNearestNeighbor)
else:
resample.SetInterpolator(sitk.sitkBSpline)
Expand Down Expand Up @@ -254,13 +261,13 @@ def resample_to_first_scan(self):
# set up resampler to resolution, field of view, etc. of first scan
resampler = sitk.ResampleImageFilter() # default linear
resampler.SetReferenceImage(self.scans[0])
resampler.SetInterpolator(sitk.sitkBSpline)
resampler.SetInterpolator(self.settings.scan_interpolator)

# resample other images
self.scans[1:] = [resampler.Execute(scan) for scan in self.scans[1:]]

# resample annotation
resampler.SetInterpolator(sitk.sitkNearestNeighbor)
resampler.SetInterpolator(self.settings.lbl_interpolator)
if self.lbl is not None:
self.lbl = resampler.Execute(self.lbl)

Expand All @@ -272,13 +279,13 @@ def resample_spacing(self, spacing: Optional[Iterable[float]] = None):

# resample scans to target resolution
self.scans = [
resample_img(scan, out_spacing=spacing, is_label=False)
resample_img(scan, out_spacing=spacing, interpolation=self.settings.scan_interpolator)
for scan in self.scans
]

# resample annotation to target resolution
if self.lbl is not None:
self.lbl = resample_img(self.lbl, out_spacing=spacing, is_label=True)
self.lbl = resample_img(self.lbl, out_spacing=spacing, interpolation=self.settings.lbl_interpolator)

def centre_crop_or_pad(self):
"""Centre crop and/or pad scans and label"""
Expand Down

0 comments on commit 87a0e39

Please sign in to comment.