-1

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?

Keren Meron
  • 139
  • 3
  • 14

1 Answers1

0

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.

Karl
  • 111
  • 10
  • My mri.shape returns a tuple of size 3: (22,512,512), without a fourth element as you say. Are there different types of nifti files? – Keren Meron Nov 02 '17 at 08:18
  • That's really odd. Have you opened the nii series in some sort of viewer and confirmed the quality of the conversion from DICOM? – Karl Nov 02 '17 at 12:28
  • How do you confirm the quality? I used nibabel library: nibabel.load(nii_path) – Keren Meron Nov 02 '17 at 16:12