Calculate volumes counting voxels, is a correct approach?


I have to calculate the volume of some lesion masks and I was wondering if counting the voxels != 0 and multiplying for the dimension of a single voxel in mm³ can be a right approach.

I mean:

  • Load the Nifti with nibabel
  • Iterating x,y,z and counting the voxel != 0
  • Multiplying for pixmap[1] * pixmap[2] + pixmap[3] from Nifti header
1 Like

@aorco you could do something like:

import nibabel as nb
import numpy as np
img = nb.load("path/to/img.nii")
num_vox = np.sum(img.get_data() > 0)

In the case you have FSL installed, you can use fslstats to compute that for you.

The usage syntax would be:

$ fslstats input.nii.gz -V

Note the capital V. This will output two numbers, the first is the number of voxels greater than 0, the second is their volume in cubic millimiters. Ex:

$fslstats $FSLDIR/data/standard/LowerCingulum_1mm.nii.gz -V
10964 10964.166992

You can check the pixel dimensions of the file to see that the volume calculation is correct.

1 Like

Is there already a python implementation / wrapper for this?
Would be happy to write one otherwise. In which package would this fit best?

If you’re making a general equivalent to fslstats or fslmaths, then nibabel would be a reasonable place for these. We have a number of CLI tools following the pattern nib-*.

for now I was thinking of simply reimplementing fslstats -V
will have a look where it could fit in

added a draft PR to nibabel:

1 Like