From 2eb4e3bfac39e19a7e3dabb3aad27ef94ca83fa8 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Fri, 22 Sep 2017 11:16:02 +0100 Subject: [PATCH] New convenience method Image.resample --- fsl/data/image.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/fsl/data/image.py b/fsl/data/image.py index 678db9f19..7a60d3ec3 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``. -- GitLab