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.
Asked
Active
Viewed 5,361 times
2
-
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 Answers
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
-
Here are some examples from NiBabel: https://nipy.org/nibabel/nibabel_images.html – Alfredo Morales Pinzón Dec 28 '22 at 23:40
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

HockeyStick
- 73
- 5
-
in Nibabel you get the header of the image, then you get spacing through : `header.get_zooms()` – Bilal Jun 05 '20 at 17:43