Skip to content

Commit f77fbb5

Browse files
committed
RF: Update conformation to reorient, rescale and resample
1 parent 241f58f commit f77fbb5

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

nibabel/processing.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from .optpkg import optional_package
2222
spnd, _, _ = optional_package('scipy.ndimage')
2323

24-
from .affines import AffineError, to_matvec, from_matvec, append_diag
24+
from .affines import AffineError, to_matvec, from_matvec, append_diag, rescale_affine
2525
from .spaces import vox2out_vox
2626
from .nifti1 import Nifti1Image
2727
from .orientations import axcodes2ornt, io_orientation, ornt_transform
@@ -373,19 +373,18 @@ def conform(from_img,
373373
elif len(voxel_size) != required_ndim:
374374
raise ValueError("`voxel_size` must have {} values".format(required_ndim))
375375

376-
# Create template image to which input is resampled.
377-
tmpl_hdr = from_img.header_class().from_header(from_img.header)
378-
tmpl_hdr.set_data_shape(out_shape)
379-
tmpl_hdr.set_zooms(voxel_size)
380-
tmpl = from_img.__class__(np.empty(out_shape), affine=np.eye(4), header=tmpl_hdr)
376+
start_ornt = io_orientation(from_img.affine)
377+
end_ornt = axcodes2ornt(orientation)
378+
transform = ornt_transform(start_ornt, end_ornt)
379+
380+
# Reorient first to ensure shape matches expectations
381+
reoriented = from_img.as_reoriented(transform)
382+
383+
out_aff = rescale_affine(reoriented.affine, reoriented.shape, voxel_size, out_shape)
381384

382385
# Resample input image.
383386
out_img = resample_from_to(
384-
from_img=from_img, to_vox_map=tmpl, order=order, mode="constant",
387+
from_img=from_img, to_vox_map=(out_shape, out_aff), order=order, mode="constant",
385388
cval=cval, out_class=out_class)
386389

387-
# Reorient to desired orientation.
388-
start_ornt = io_orientation(out_img.affine)
389-
end_ornt = axcodes2ornt(orientation)
390-
transform = ornt_transform(start_ornt, end_ornt)
391-
return out_img.as_reoriented(transform)
390+
return out_img

0 commit comments

Comments
 (0)