Skip to content

Commit 4ab6617

Browse files
committed
Add interpolator parameter with sitkBSpline as default
1 parent e5d5d75 commit 4ab6617

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

delia/transforms/physical_space/matching_resample.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
@Description: This file contains the MatchingResampled transform.
99
"""
1010

11-
from typing import Dict, Hashable
11+
from typing import Callable, Dict, Hashable
1212

1313
import SimpleITK as sitk
1414

15-
from delia.transforms.physical_space.transform import PhysicalSpaceTransform, ImageData, KeysCollection
15+
from delia.transforms.physical_space.transform import PhysicalSpaceTransform, ImageData, KeysCollection, Mode
1616

1717

1818
class MatchingResampled(PhysicalSpaceTransform):
@@ -24,6 +24,7 @@ def __init__(
2424
self,
2525
reference_image_key: str,
2626
matching_keys: KeysCollection,
27+
interpolator: Callable = sitk.sitkBSpline,
2728
):
2829
"""
2930
Initializes images.
@@ -37,10 +38,13 @@ def __init__(
3738
for foreground on the image. Image keys are assumed to be arbitrary series keys defined in
3839
'series_descriptions'. For the label maps, the keys are organ names. Note that if 'series_descriptions' is
3940
None, the image keys are assumed to be modality names.
41+
interpolator : Callable
42+
The interpolator to be used for resampling the images. Default = sitk.sitkBSpline.
4043
"""
4144
keys = [key for key in matching_keys] + [reference_image_key]
4245
super().__init__(keys=keys)
4346
self._reference_image_key = reference_image_key
47+
self._interpolator = interpolator
4448

4549
def __call__(self, data: Dict[str, ImageData]) -> Dict[Hashable, sitk.Image]:
4650
"""
@@ -65,7 +69,21 @@ def __call__(self, data: Dict[str, ImageData]) -> Dict[Hashable, sitk.Image]:
6569

6670
for key in self.key_iterator(d):
6771
if key != self._reference_image_key:
68-
d[key] = sitk.Resample(d[key].simple_itk_image, reference_image)
72+
73+
if self._mode == Mode.NONE:
74+
raise AssertionError("Transform mode must be set before __call__.")
75+
elif self._mode == Mode.IMAGE:
76+
interpolator = self._interpolator
77+
elif self._mode == Mode.SEGMENTATION:
78+
interpolator = sitk.sitkNearestNeighbor
79+
else:
80+
raise ValueError("Unknown transform mode.")
81+
82+
d[key] = sitk.Resample(
83+
image1=d[key].simple_itk_image,
84+
referenceImage=reference_image,
85+
interpolator=interpolator
86+
)
6987

7088
return d
7189

delia/transforms/physical_space/resample.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@Description: This file contains the Resampled transform.
99
"""
1010

11-
from typing import Dict, Hashable, Tuple
11+
from typing import Callable, Dict, Hashable, Tuple
1212

1313
import SimpleITK as sitk
1414
import numpy as np
@@ -24,7 +24,8 @@ class Resampled(PhysicalSpaceTransform):
2424
def __init__(
2525
self,
2626
keys: KeysCollection,
27-
out_spacing: Tuple[float, float, float] = (1.0, 1.0, 1.0)
27+
out_spacing: Tuple[float, float, float] = (1.0, 1.0, 1.0),
28+
interpolator: Callable = sitk.sitkBSpline,
2829
):
2930
"""
3031
Initialize output spacing.
@@ -37,9 +38,12 @@ def __init__(
3738
'series_descriptions' is None, the image keys are assumed to be modality names.
3839
out_spacing : Tuple[int, int, int], default = (1.0, 1.0, 1.0)
3940
The desired spacing in the physical space. Default = (1.0 mm, 1.0 mm, 1.0 mm).
41+
interpolator : Callable
42+
The interpolator to be used for resampling the images. Default = sitk.sitkBSpline.
4043
"""
4144
super().__init__(keys=keys)
4245
self._out_spacing = out_spacing
46+
self._interpolator = interpolator
4347

4448
def __call__(self, data: Dict[str, ImageData]) -> Dict[Hashable, sitk.Image]:
4549
"""
@@ -80,7 +84,7 @@ def __call__(self, data: Dict[str, ImageData]) -> Dict[Hashable, sitk.Image]:
8084
if self._mode == Mode.NONE:
8185
raise AssertionError("Transform mode must be set before __call__.")
8286
elif self._mode == Mode.IMAGE:
83-
resample.SetInterpolator(sitk.sitkLinear)
87+
resample.SetInterpolator(self._interpolator)
8488
elif self._mode == Mode.SEGMENTATION:
8589
resample.SetInterpolator(sitk.sitkNearestNeighbor)
8690
else:

0 commit comments

Comments
 (0)