I have a nii file of a kidney which I upload in Python. Is there any function in itk or vtk to calculate a volume of the kidney?
Asked
Active
Viewed 2,374 times
2 Answers
1
I share with you a solution with SimpleITK.
You require a mask image of the kidney. The mask is a binary image with 1 values where the organ is and 0 values otherwise. If you don't have this already you can use 3DSlicer to segment the image, see here: 3DSlicer Segmentation. After this you will have a kidney_mask.nii (or nrrd, default format) file.
You can use the following script to compute the volume.
# script file image_volume.py
import argparse # argument parser
import numpy as np
import SimpleITK as sitk
def volume( mask_image ):
# Input:
# image = sitk.Image, mask or binary image (1 values where organ, 0 values otherwise)
# Output:
# vol = float, volume in mm3
space = mask_image.GetSpacing() # image spacing
voxel = np.prod(space) # voxel volume
img = sitk.GetArrayFromImage(mask_image)
vol = voxel*np.sum(img)
return vol
def main():
# Arguments details
parser = argparse.ArgumentParser(description='Compute volume of a mask image')
parser.add_argument("input", type=str,
help='Input file name')
# Parse arguments
args = parser.parse_args()
image_file = args.input
# Read the image
try:
image = sitk.ReadImage(image_file)
except:
print('Unable to read input image file.')
# Calculate the volume
print('Volume: {:f} [mm3]'.format(volume(image)))
print('Volume: {:f} [cm3]'.format(volume(image)/1000.0))
return
if __name__ == "__main__":
# execute only if run as a script
main()
Call the script as:
python image_volume.py '/path/folder/kidney_mask.nii'

José D. Tascón-Vidarte
- 166
- 8
0
Here's how you can do it in just SimpleITK (the other answer uses numpy for the voxel count)
import SimpleITK as sitk
stats = sitk.StatisticsImageFilter()
# img is a SimpleITK image
stats.Execute(img)
# get the number of voxels with label 1
nvoxels = stats.GetCount(1)
spacing = img.GetSpacing()
voxvol = spacing[0]*spacing[1]*spacing[2]
volume = nvoxels * voxvol

Dave Chen
- 1,905
- 1
- 12
- 18
-
Using this code I have an error "Execute() missing 1 required positional argument: 'labelImage'" which refers to stats.Execute(img) – Koko3100 Jun 02 '21 at 10:11
-
Sorry, I had the wrong filter. I've updated the code to use StatsticsImageFilter, not LabelStatisticsImageFilter. – Dave Chen Jun 02 '21 at 14:32