Skip to content

Commit 587a15b

Browse files
authored
Merge pull request #549 from oesteban/enh/smoothing-regridtozooms
ENH: Add ``smooth`` input to ``RegridToZooms``
2 parents 43adaf0 + f52c7fb commit 587a15b

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
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+
traits.Bool(),
47+
traits.Float(),
48+
default=False,
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: 9 additions & 4 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
@@ -163,8 +163,6 @@ def resample_by_spacing(in_file, zooms, order=3, clip=True):
163163
qform, qcode = in_file.get_qform(coded=True)
164164

165165
hdr = in_file.header.copy()
166-
dtype = hdr.get_data_dtype()
167-
data = np.asanyarray(in_file.dataobj)
168166
zooms = np.array(zooms)
169167

170168
# Calculate the factors to normalize voxel size to the specific zooms
@@ -195,11 +193,18 @@ def resample_by_spacing(in_file, zooms, order=3, clip=True):
195193
new_card.dot(np.vstack((new_grid, np.ones((1, new_grid.shape[1])))))
196194
)
197195

196+
if smooth:
197+
from scipy.ndimage import gaussian_filter
198+
if smooth is True:
199+
smooth = np.maximum(0, (pre_zooms / zooms - 1) / 2)
200+
data = gaussian_filter(in_file.get_fdata(), smooth)
201+
else:
202+
data = np.asarray(in_file.dataobj)
203+
198204
# Resample data in the new grid
199205
resampled = map_coordinates(
200206
data,
201207
ijk[:3, :],
202-
output=dtype,
203208
order=order,
204209
mode="constant",
205210
cval=0,

0 commit comments

Comments
 (0)