[Neuroimaging] nibabel question

Christopher Markiewicz effigies at bu.edu
Sat Oct 14 17:05:31 EDT 2017


I think there's a little bit of a DICOM-NIfTI vocabulary mismatch here. In
my mind, a NIfTI file is an "image", and contains one or more "volumes" of
a given size. The three spatial dimensions may be called x, y, and z, and
the affine matrix encoded by the header translates voxel indices along
these dimensions into coordinates in RAS space, which is millimeters to the
right, anterior and superior of an origin (usually somewhere in the brain).
x, y and z may correspond to phase-, frequency- and slice-encoding
directions. Even if you don't have phase/frequency directions, usually you
have slices.

>From your email, I'm guessing you're using both "frame" and "image" to
refer to what I'd call a slice, or a 2D matrix that, when stacked in
series, produce a volume. So I'm interpreting your question as "How do I
determine the number of slices in a NIfTI image." which really hinges upon
determining the slice direction.

Assuming you've loaded a file:

>>> img = nib.load(fname)

You can look at the shape (as you have):

>>> img.shape
(x, y, z)

This might be enough, as often the slice dimension is different from the
other two, which are the same size. You can also look at the zooms:

>>> img.header.get_zooms()
(1.0, 1.0, 1.0)

Zooms are the width of the voxels in each direction. Again, often the slice
dimension is the odd one out. You can also look at orientation information:

>>> nib.aff2axcodes(img.afffine)  # Will give some sequence of R/L, A/P,
S/I, e.g.
('L', 'A', 'S')

If you know your slice direction was superior/inferior, then the
z-direction is what you want, and you can now go back to `img.shape` and
see how many slices you had.

Finally, there is a chance that the slice dimension is actually encoded in
the NIfTI header:

>>> freq_dim, phase_dim, slice_dim = img.header.get_dim_info()

If slice_dim is not `None`, then you can get the number of slices with

>>> img.shape[slice_dim]

Hopefully the vocabulary mismatch didn't send me off in entirely the wrong
direction, and this was somewhat helpful.

-- 
Chris Markiewicz


On Sat, Oct 14, 2017 at 12:42 PM, Keren Meron <keren.meron at mail.huji.ac.il>
wrote:

> 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?
>
>
> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Virus-free.
> www.avg.com
> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> <#m_8563802930650991843_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> _______________________________________________
> Neuroimaging mailing list
> Neuroimaging at python.org
> https://mail.python.org/mailman/listinfo/neuroimaging
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/neuroimaging/attachments/20171014/fd26e93e/attachment.html>


More information about the Neuroimaging mailing list