Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying Affine not affecting get_fdata() output #660

Closed
kleingeo opened this issue Sep 8, 2018 · 4 comments
Closed

Modifying Affine not affecting get_fdata() output #660

kleingeo opened this issue Sep 8, 2018 · 4 comments

Comments

@kleingeo
Copy link

kleingeo commented Sep 8, 2018

I am trying to manipulate an image by modifying its affine matrix. When I modify it and plot the output using the OrthoSlicer3D class, it is properly modified as the slices are oriented differently. However, when I try to look at a raw image slice output using get_fdata() and matplotlib, nothing changes. I am not sure exactly what I am doing wrong. I am currently working with Nifti1Image class images.

@matthew-brett
Copy link
Member

The affine is metadata for the image - it records the relationship of coordinates in the image array to coordinates in some real space - see:

http://nipy.org/nibabel/coordinate_systems.html

So - changing the affine doesn't change the underlying image, it only changes the metadata for the image.

@kleingeo
Copy link
Author

kleingeo commented Sep 9, 2018

@matthew-brett I figured that much, I used the nib.orientations.apply_orientation to get the orientation for the plots right. Is there another way to apply changes to affine to the image data itself?

Also, side question, when using nib.viewers.OrthoSlicer3D, is there a way to display to different images overlayed, like a main image and corresponding mask?

@matthew-brett
Copy link
Member

Sorry - we don't support multiple images in OrthoSlicer3D - but it would be a good feature to add ...

If you want to resample the image to some orientation, set the affine as you want it, make an affine for the output image, maybe a simple diagonal one, and resample with that. Something like:

img = nib.load('original.nii')
# get_base_affine gives the simplest diagonal voxels to mm mapping.
# You can use any affine you want - it will be the affine for the new image.
new_affine = img.header.get_base_affine()
# Get resampling routine.
from nibabel.processing import resample_from_to
# Resample image to new affine with default parameters (e.g. trilinear resampling).
new_img = resample_from_to(img, (img.shape, new_affine))
nib.save(new_img, 'reoriented.nii')

@effigies
Copy link
Member

I opened a new issue for overlays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants