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


@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.