@@ -148,7 +148,7 @@ def dseg_label(in_seg, label, newpath=None):
148
148
return out_file
149
149
150
150
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 ):
152
152
"""Regrid the input image to match the new zooms."""
153
153
from pathlib import Path
154
154
import numpy as np
@@ -163,8 +163,6 @@ def resample_by_spacing(in_file, zooms, order=3, clip=True):
163
163
qform , qcode = in_file .get_qform (coded = True )
164
164
165
165
hdr = in_file .header .copy ()
166
- dtype = hdr .get_data_dtype ()
167
- data = np .asanyarray (in_file .dataobj )
168
166
zooms = np .array (zooms )
169
167
170
168
# 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):
195
193
new_card .dot (np .vstack ((new_grid , np .ones ((1 , new_grid .shape [1 ])))))
196
194
)
197
195
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
+
198
204
# Resample data in the new grid
199
205
resampled = map_coordinates (
200
206
data ,
201
207
ijk [:3 , :],
202
- output = dtype ,
203
208
order = order ,
204
209
mode = "constant" ,
205
210
cval = 0 ,
0 commit comments