Reputation: 70
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.
Upvotes: 2
Views: 7996
Reputation: 693
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
Upvotes: 9
Reputation: 73
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
Upvotes: 1