[Neuroimaging] Looping through 3D NIFTI Image: Help

Matthew Brett matthew.brett at gmail.com
Fri Jan 4 16:13:33 EST 2019


On Fri, Jan 4, 2019 at 4:58 PM Arnav Lohe <arnavlohe15 at gmail.com> wrote:
> Dear Neuroimaging List Members,
> My name is Arnav Lohe, and I am a junior at Rutgers University working on a neuroimaging project. I want to write an original piece of code to calculate the total tissue volume in a (.nii) MRI image, and to do this I need to loop through the multidimensional array. The loop is what I do not know how to do. I tried triple indexing, I tried converting the .nii image into a numpy array, and got nowhere with my approaches. Does someone know how to loop through the individual voxels and extract the numerical information inside each one?

Just to add to other replies, it is almost always better to vectorize
- I mean - use Numpy arrays to do multiple operations at once.

For example, if your image is a grey matter probability image, and you
just want the sum of the voxels:

img = nib.load('my_image.nii')
data = img.get_fdata()

Or you want to do a histogram:

import matplotlib.pyplot as plt

I don't myself do StackOverflow or Neurostars, so for nibabel
questions, at least, this list is a good place to ask.



More information about the Neuroimaging mailing list