2

I have loaded a nifti image file using nibabel tool and I have played with some properties. But I don’t have idea how to compute the volume (in mm³) of a single voxel.

Carlao
  • 74
  • 1
  • 5
  • Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/help/how-to-ask). Please [edit](https://stackoverflow.com/posts/62183303/edit) the post to include your own effort into solving this problem. The latter preferably in code, this is called a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Bilal Jun 04 '20 at 10:19
  • *compute the volume (in mm³) of a single voxel* you can compute the **count of pixels in each slice**, then you **multiply** your pixels count with **pixel spacing**, if you can provide the code you used o read your data, with a sample of your Data, with details about the organ you want to calculate the volume for it, then maybe I can help more. – Bilal Jun 04 '20 at 10:22

2 Answers2

7

Here's the answer using NiBabel, as OP asked:

import nibabel as nib
nii = nib.load('t1.nii.gz')
sx, sy, sz = nii.header.get_zooms()
volume = sx * sy * sz
fepegar
  • 595
  • 5
  • 15
1

I am not a NiBabel expert, but I can instead recommend the SimpleITK package for Python. I often use it for reading NifTi image files. It has a method GetSpacing() which returns the pixel spacing in mm.

import SimpleITK as sitk

# read image
im = sitk.ReadImage("/path/to/input/image.nii")

# get voxel spacing (for 3-D image)
spacing = im.GetSpacing()
spacing_x = spacing[0]
spacing_y = spacing[1]
spacing_z = spacing[2]

# determine volume of a single voxel
voxel_volume = spacing_x * spacing_y * spacing_z
  • in Nibabel you get the header of the image, then you get spacing through : `header.get_zooms()` – Bilal Jun 05 '20 at 17:43