diff --git a/fsl/data/image.py b/fsl/data/image.py index 678db9f1932ac1661f0952626e5c0030df20ef75..7a60d3ec3e63f47f2e10ab031c134df3b52baac6 100644 --- a/fsl/data/image.py +++ b/fsl/data/image.py @@ -40,6 +40,7 @@ import logging import six import deprecation import numpy as np +import scipy.ndimage as ndimage import nibabel as nib import nibabel.fileslice as fileslice @@ -1115,6 +1116,33 @@ class Image(Nifti): self.notify(topic='saveState') + def resample(self, shape, sliceobj=None, **kwargs): + """Returns a copy of the data in this ``Image``, resampled to the + specified ``shape``. + + :arg shape: Desired shape + + :arg sliceobj: Slice into this ``Image``. If ``None``, the whole + image is resampled, and it is assumed that it has the + same number of dimensions as ``shape``. + + All other arguments are passed through to the ``scipy.ndimage.zoom`` + function. + """ + + if sliceobj is None: + sliceobj = slice(None) + + ndims = len(shape) + data = self[sliceobj] + + if tuple(data.shape) != tuple(shape): + zooms = [float(shape[i]) / data.shape[i] for i in range(ndims)] + data = ndimage.zoom(data, zooms, **kwargs) + + return data + + def __getitem__(self, sliceobj): """Access the image data with the specified ``sliceobj``.