nipy / nibabel

Python package to access a cacophony of neuro-imaging file formats
http://nipy.org/nibabel/
Other
649 stars 258 forks source link

Shape mismatch #1293

Closed monjoybme closed 7 months ago

monjoybme commented 8 months ago

Hi,

I am reading a NIFTI file using the following command: ds = nib.load(read_file).get_fdata() When I checked the shape of ds, I found (512, 512, 168). Could you please tell me how can I change this to (168, 512, 512)? Thanks in advance!

effigies commented 8 months ago

I think it depends on which dimensions you want to be in what order. If you just want to reorder a data array, you could use numpy.moveaxis, but if you need to recover the orientation information, it makes more sense to rotate/flip the image to a target orientation and then fetch the data. e.g.

import nibabel as nb

img = nb.load(fname)

src_ornt = nb.orientations.io_orientation(img.affine)
dst_ornt = nb.orientations.axcodes2ornt("RAS")  # Or whatever orientation you want
transform = nb.orientations.ornt_transform(src_ornt, dst_ornt)
reoriented = img.as_reoriented(transform)

data = reoriented.get_fdata()