1
votes

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.

2
Welcome to Stack Overflow! Please take the tour and read How to Ask. Please edit the post to include your own effort into solving this problem. The latter preferably in code, this is called a minimal reproducible example.Bilal
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

2 Answers

3
votes

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
1
votes

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