<div dir="ltr"><div>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.</div><div><br></div><div>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.</div><div><br></div><div>Assuming you've loaded a file:</div><div><br></div><div><div class="gmail_extra">>>> img = nib.load(fname)</div></div><div><br></div><div>You can look at the shape (as you have):</div><div><br></div><div class="gmail_extra">>>> img.shape<br></div><div class="gmail_extra">(x, y, z)</div><div class="gmail_extra"><br></div><div class="gmail_extra">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:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra">>>> img.header.get_zooms()</div><div class="gmail_extra">(1.0, 1.0, 1.0)</div><div><br></div><div>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:</div></div><div class="gmail_extra"><br></div><div class="gmail_extra">>>> nib.aff2axcodes(img.afffine)  # Will give some sequence of R/L, A/P, S/I, e.g.</div><div class="gmail_extra">('L', 'A', 'S')</div><div class="gmail_extra"><br></div><div class="gmail_extra">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.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Finally, there is a chance that the slice dimension is actually encoded in the NIfTI header:</div><div class="gmail_extra"><br></div><div class="gmail_extra">>>> freq_dim, phase_dim, slice_dim = img.header.get_dim_info()</div><div class="gmail_extra"><br></div><div class="gmail_extra">If slice_dim is not `None`, then you can get the number of slices with</div><div class="gmail_extra"><br></div><div class="gmail_extra">>>> img.shape[slice_dim]</div><div class="gmail_extra"><br></div><div class="gmail_extra">Hopefully the vocabulary mismatch didn't send me off in entirely the wrong direction, and this was somewhat helpful.</div><div class="gmail_extra"><br></div><div class="gmail_extra">-- </div><div class="gmail_extra">Chris Markiewicz</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Oct 14, 2017 at 12:42 PM, Keren Meron <span dir="ltr"><<a href="mailto:keren.meron@mail.huji.ac.il" target="_blank">keren.meron@mail.huji.ac.il</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><p style="margin:0px 0px 1em;padding:0px;border:0px;font-variant-numeric:inherit;font-stretch:inherit;font-size:15px;line-height:inherit;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;vertical-align:baseline;clear:both;color:rgb(36,39,41)">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).</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-variant-numeric:inherit;font-stretch:inherit;font-size:15px;line-height:inherit;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;vertical-align:baseline;clear:both;color:rgb(36,39,41)">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.</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-variant-numeric:inherit;font-stretch:inherit;font-size:15px;line-height:inherit;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;vertical-align:baseline;clear:both;color:rgb(36,39,41)">Is there a way to retrieve the number of images in a Nifti file?</p></div><div id="gmail-m_8563802930650991843DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br>
<table style="border-top:1px solid rgb(211,212,222)">
        <tbody><tr>
        <td style="width:55px;padding-top:13px"><a href="http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail" target="_blank"><img src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-green-avg-v1.png" alt="" width="46" height="29" style="width: 46px; height: 29px;"></a></td>
                <td style="width:470px;padding-top:12px;color:rgb(65,66,78);font-size:13px;font-family:Arial,Helvetica,sans-serif;line-height:18px">Virus-free. <a href="http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail" style="color:rgb(68,83,234)" target="_blank">www.avg.com</a>
                </td>
        </tr>
</tbody></table><a href="#m_8563802930650991843_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1"></a></div>
<br>______________________________<wbr>_________________<br>
Neuroimaging mailing list<br>
<a href="mailto:Neuroimaging@python.org">Neuroimaging@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/neuroimaging" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/neuroimaging</a><br>
<br></blockquote></div><br></div></div>