# How does 3d slicer calculate the segmentation volume of dicom image？

**URL:** <https://discourse.slicer.org/t/how-does-3d-slicer-calculate-the-segmentation-volume-of-dicom-image/28596>\
**Category:** Development\
**Created:** [March 27, 2023, 12:23pm UTC](https://discourse.slicer.org/t/how-does-3d-slicer-calculate-the-segmentation-volume-of-dicom-image/28596 "2023-03-27T12:23:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyh-ecnu](https://avatars.discourse-cdn.com/v4/letter/l/51bf81/32.png) [@lyh-ecnu](https://discourse.slicer.org/u/lyh-ecnu)\
**Post date:** [March 27, 2023, 12:23pm UTC](https://discourse.slicer.org/t/how-does-3d-slicer-calculate-the-segmentation-volume-of-dicom-image/28596/1 "2023-03-27T12:23:53Z")

</div>

hello,I have a project that requires a lot of calculations about the segmentation volume of dicom image.Firstly I tried to use python’s simpleitk library to calculate it,My code looks roughly like this：  
file = sitk.ReadImage(f’{path}{file}')  
spacing = file.GetSpacing()  
file = sitk.GetArrayFromImage(file)  
file=np.array(file)  
file = file.astype(int)  
#print(spacing)  
sum=file.sum()  
#print(sum)  
v=sum\*spacing[0]\*spacing[1]\*spacing[2]/1000  
print(v)  
But the volume calculated in this way is not the same as the volume calculated by slicer，Where is my code going wrong? Or is there where I can see the source code for slicer to do this?thanks

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 27, 2023, 3:28pm UTC](https://discourse.slicer.org/t/how-does-3d-slicer-calculate-the-segmentation-volume-of-dicom-image/28596/2 "2023-03-27T15:28:22Z")

</div>

Segment statistics volume in Slicer can compute volume from binary labelmap and closed surface representations. Probably you are interested in the binary labelmap representation, from that the volume for each segment is computed by multiplying the number of voxels in the segment by the voxel size.

The error in your code is that the sum of label values is used instead count the number of non-zero voxels (or number of voxels that have a certain label value). I would also recommend to include the unit name in the variable names (e.g., spacing\_mm, volume\_mm3 or volume\_cm3) to make the code more readable, safer, and easier to explain magic numbers, such as the 1000. Using `file` for so many unrelated this is quite confusing, too.

---

<div class="post-metadata">

**Author:** ![lyh-ecnu](https://avatars.discourse-cdn.com/v4/letter/l/51bf81/32.png) [@lyh-ecnu](https://discourse.slicer.org/u/lyh-ecnu)\
**Post date:** [March 28, 2023, 5:06am UTC](https://discourse.slicer.org/t/how-does-3d-slicer-calculate-the-segmentation-volume-of-dicom-image/28596/3 "2023-03-28T05:06:52Z")

</div>

thanks for you answer
