diff --git a/scilpy/image/volume_operations.py b/scilpy/image/volume_operations.py index 5f6586f37..2b69af494 100644 --- a/scilpy/image/volume_operations.py +++ b/scilpy/image/volume_operations.py @@ -452,7 +452,7 @@ def _interp_code_to_order(interp_code): return orders[interp_code] -def resample_volume(img, ref=None, res=None, iso_min=False, zoom=None, +def resample_volume(img, ref_img=None, res=None, iso_min=False, zoom=None, interp='lin', enforce_dimensions=False): """ Function to resample a dataset to match the resolution of another @@ -463,7 +463,7 @@ def resample_volume(img, ref=None, res=None, iso_min=False, zoom=None, ---------- img: nib.Nifti1Image Image to resample. - ref: nib.Nifti1Image + ref_img: nib.Nifti1Image, optional Reference volume to resample to. This method is used only if ref is not None. (default: None) res: tuple, shape (3,) or int, optional @@ -492,11 +492,10 @@ def resample_volume(img, ref=None, res=None, iso_min=False, zoom=None, affine = img.affine original_zooms = img.header.get_zooms()[:3] - if ref is not None: + if ref_img is not None: if iso_min or zoom or res: raise ValueError('Please only provide one option amongst ref, res ' ', zoom or iso_min.') - ref_img = nib.load(ref) new_zooms = ref_img.header.get_zooms()[:3] elif res is not None: if iso_min or zoom: diff --git a/scripts/scil_volume_resample.py b/scripts/scil_volume_resample.py index 1c74e005d..8524831fb 100755 --- a/scripts/scil_volume_resample.py +++ b/scripts/scil_volume_resample.py @@ -12,6 +12,7 @@ import logging import nibabel as nib +import numpy as np from scilpy.io.utils import (add_verbose_arg, add_overwrite_arg, assert_inputs_exist, assert_outputs_exist, @@ -67,8 +68,6 @@ def main(): # Checking args assert_inputs_exist(parser, args.in_image, args.ref) assert_outputs_exist(parser, args, args.out_image) - assert_headers_compatible(parser, args.in_image, args.ref) - assert_headers_compatible(parser, args.in_image, args.ref) if args.enforce_dimensions and not args.ref: parser.error("Cannot enforce dimensions without a reference image") @@ -85,8 +84,17 @@ def main(): img = nib.load(args.in_image) + ref_img = None + if args.ref: + ref_img = nib.load(args.ref) + # Must not verify that headers are compatible. But can verify that, at + # least, that the last column of their affine are compatible. + if not np.array_equal(img.affine[:, -1], ref_img.affine[:, -1]): + parser.error("The --ref image should have the same affine as the " + "input image (but with a different sampling).") + # Resampling volume - resampled_img = resample_volume(img, ref=args.ref, res=args.volume_size, + resampled_img = resample_volume(img, ref_img=ref_img, res=args.volume_size, iso_min=args.iso_min, zoom=args.voxel_size, interp=args.interp, enforce_dimensions=args.enforce_dimensions)