Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions niworkflows/interfaces/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class _RegridToZoomsInputSpec(BaseInterfaceInputSpec):
usedefault=True,
desc="clip the data array within the original image's range",
)
smooth = traits.Either(
traits.Bool(),
traits.Float(),
default=False,
usedefault=True,
desc="apply gaussian smoothing before resampling"
)


class _RegridToZoomsOutputSpec(TraitedSpec):
Expand All @@ -65,6 +72,7 @@ def _run_interface(self, runtime):
self.inputs.zooms,
order=self.inputs.order,
clip=self.inputs.clip,
smooth=self.inputs.smooth,
).to_filename(self._results["out_file"])
return runtime

Expand Down
13 changes: 9 additions & 4 deletions niworkflows/utils/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def dseg_label(in_seg, label, newpath=None):
return out_file


def resample_by_spacing(in_file, zooms, order=3, clip=True):
def resample_by_spacing(in_file, zooms, order=3, clip=True, smooth=False):
"""Regrid the input image to match the new zooms."""
from pathlib import Path
import numpy as np
Expand All @@ -163,8 +163,6 @@ def resample_by_spacing(in_file, zooms, order=3, clip=True):
qform, qcode = in_file.get_qform(coded=True)

hdr = in_file.header.copy()
dtype = hdr.get_data_dtype()
data = np.asanyarray(in_file.dataobj)
zooms = np.array(zooms)

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

if smooth:
from scipy.ndimage import gaussian_filter
if smooth is True:
smooth = np.maximum(0, (pre_zooms / zooms - 1) / 2)
data = gaussian_filter(in_file.get_fdata(), smooth)
else:
data = np.asarray(in_file.dataobj)

# Resample data in the new grid
resampled = map_coordinates(
data,
ijk[:3, :],
output=dtype,
order=order,
mode="constant",
cval=0,
Expand Down