|
21 | 21 | from .optpkg import optional_package |
22 | 22 | spnd, _, _ = optional_package('scipy.ndimage') |
23 | 23 |
|
24 | | -from .affines import AffineError, to_matvec, from_matvec, append_diag |
| 24 | +from .affines import AffineError, to_matvec, from_matvec, append_diag, rescale_affine |
25 | 25 | from .spaces import vox2out_vox |
26 | 26 | from .nifti1 import Nifti1Image |
27 | 27 | from .orientations import axcodes2ornt, io_orientation, ornt_transform |
@@ -373,19 +373,18 @@ def conform(from_img, |
373 | 373 | elif len(voxel_size) != required_ndim: |
374 | 374 | raise ValueError("`voxel_size` must have {} values".format(required_ndim)) |
375 | 375 |
|
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) |
381 | 384 |
|
382 | 385 | # Resample input image. |
383 | 386 | 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", |
385 | 388 | cval=cval, out_class=out_class) |
386 | 389 |
|
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