简体   繁体   中英

How to get number of images in NIfTi object? (nibabel)

I have a Nifti object generated from a directory of dicom files. It seems that the Nifti should know how many frames it holds, but all I can find in the header info is the shape. The problem is, the shape is at times (num_images, x, y) and at times (x, y, num_images).

The only nibabel functions I found relevant where from the Ecat library. I am not familiar with ecat format, but I want my method to work for any nii file. I am working with the nibabel library.

Is there a way to retrieve the number of images in a Nifti file?

I'm guessing you're looking at fMRI, DTI or ASL data.

Say your 4D nii stack is called 'data.nii.'

Just go into that directory and do:

mri = nib.load('data.nii')

mri.shape

The fourth element you see will be the number of volumes. You can access it thusly: mri.shape[3] if you need it for some kind of purpose in your programs.

This works consistently for me. If your data are "stacked" in an inconsistent orientation, you are going to have to get fancy.

You could include checks based off of the dimensionality of your images. For example if you know that your images are 128x128x128, then you can go ahead and get whichever element of mri.shape isn't 128, but this approach is suboptimal for a few reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM