@@ -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