Skip to content

Commit 001f4be

Browse files
committed
ENH: Add smooth input to RegridToZooms
Resampling (up- or down-) typically requires a gussian smoothing to be run before, so that outliers are not introduced (esp. when downsampling). This PR adds this option, relying on scipy.
1 parent 5153624 commit 001f4be

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

niworkflows/interfaces/images.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class _RegridToZoomsInputSpec(BaseInterfaceInputSpec):
4242
usedefault=True,
4343
desc="clip the data array within the original image's range",
4444
)
45+
smooth = traits.Either(
46+
False,
47+
traits.Bool(),
48+
traits.Float(),
49+
usedefault=True,
50+
desc="apply gaussian smoothing before resampling"
51+
)
4552

4653

4754
class _RegridToZoomsOutputSpec(TraitedSpec):
@@ -65,6 +72,7 @@ def _run_interface(self, runtime):
6572
self.inputs.zooms,
6673
order=self.inputs.order,
6774
clip=self.inputs.clip,
75+
smooth=self.inputs.smooth,
6876
).to_filename(self._results["out_file"])
6977
return runtime
7078

niworkflows/utils/images.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def dseg_label(in_seg, label, newpath=None):
148148
return out_file
149149

150150

151-
def resample_by_spacing(in_file, zooms, order=3, clip=True):
151+
def resample_by_spacing(in_file, zooms, order=3, clip=True, smooth=False):
152152
"""Regrid the input image to match the new zooms."""
153153
from pathlib import Path
154154
import numpy as np
@@ -164,7 +164,6 @@ def resample_by_spacing(in_file, zooms, order=3, clip=True):
164164

165165
hdr = in_file.header.copy()
166166
dtype = hdr.get_data_dtype()
167-
data = np.asanyarray(in_file.dataobj)
168167
zooms = np.array(zooms)
169168

170169
# Calculate the factors to normalize voxel size to the specific zooms
@@ -195,6 +194,13 @@ def resample_by_spacing(in_file, zooms, order=3, clip=True):
195194
new_card.dot(np.vstack((new_grid, np.ones((1, new_grid.shape[1])))))
196195
)
197196

197+
if smooth:
198+
from scipy.ndimage import gaussian_filter
199+
data = gaussian_filter(in_file.get_fdata(),
200+
2 if smooth is True else smooth).astype(dtype)
201+
else:
202+
data = np.asanyarray(in_file.dataobj)
203+
198204
# Resample data in the new grid
199205
resampled = map_coordinates(
200206
data,

0 commit comments

Comments
 (0)