I’m trying to extract two volumes with nibabel
slicer
:
>> image.slicer(:,:,:,[0,1])
but this throws an exception
IndexError: Cannot handle fancy indexing
I’m trying to extract two volumes with nibabel
slicer
:
>> image.slicer(:,:,:,[0,1])
but this throws an exception
IndexError: Cannot handle fancy indexing
Hi @mri!
In this simple case you could use the following:
>>> image.slicer[..., :2]
since the indices you want are consecutive.
If you have more complex cases though where the slices you want are non-consecutive and can’t be generated by e.g., a slice()
instance you may need to coerce the underlying data object to a numpy array, fancy-index that, and then regenerate the image, e.g.:
>>> idx = [0, 5, 6, 10]
>>> data = np.asarray(image.dataobj)[..., idx]
>>> image = image.__class__(data, affine=image.affine, header=image.header, extra=image.extra)
Hope that helps!
MANAGED BY INCF