[Neuroimaging] Niftiimage Affine Rotation

Matthew Brett matthew.brett at gmail.com
Fri Jul 12 08:52:59 EDT 2019


Hi,


On Fri, Jul 12, 2019 at 5:29 AM Kai Canoll <kaicanoll at gmail.com> wrote:
>
>
> I would like to apply an affine transform to my image, then open the transformed image in python to do additional processing.  In most other toolboxes, the affine transformation and the resampling are separate steps, i.e. you can rotate an image without changing the resolution or you can change the resolution without rotating.
> Is there any way to just apply an affine transform to an image in nibabel?

The image, in Nibabel as in other implementations, is the assocation
of the (array of data, the header).  The header contains the affine.

You can change the array of data without changing the header, and vice versa.

I am not sure what you mean, exactly, by "apply an affine transform",
but the obvious meaning, is setting or modifying the affine transform
of the image (which then goes into the header), without modifying the
image data.

You can do that by making a new image, with the affine you want:

>>> img = nib.load('my_image.nii')
>>> new_img = nib.Nifti1Image(img.dataobj, my_new_affine, img.header)
>>> nib.save(new_image, 'my_image_modified_affine.nii')

If you want to change the image data by applying such a rotation, then
you'll need to resample, in Nibabel as for any other software.

Cheers,

Matthew


More information about the Neuroimaging mailing list