Is there a way to get the volume of just one segment within a segmentation?

Apologies if this is fairly obvious, learning python has been a steep learning curve. I have a segmentation that includes two segments: blood and bone. Using the docs, I’ve been able to find code that automatically prints the volume of both:

import SegmentStatistics
segStatLogic = SegmentStatistics.SegmentStatisticsLogic()

segStatLogic.getParameterNode().SetParameter(“ScalarVolume”, volumeNode.GetID())

segStatLogic.getParameterNode().SetParameter(“Segmentation”, segmentationNode.GetID())
segStatLogic.computeStatistics()
stats = segStatLogic.getStatistics()

for segmentId in stats[“SegmentIDs”]:
volume_mm3 = stats[segmentId,“LabelmapSegmentStatisticsPlugin.volume_mm3”]
segmentName = segmentationNode.GetSegmentation().GetSegment(segmentId).GetName()
print(f"{segmentName} volume = {volume_mm3} mm3")

However, is there a way to return ONLY one of the segment volumes from a segmentation? For example:

stats[“SegmentIDs”][0] correponds to the blood segment, and stats[“SegmentIDs”][1] to the bone segment. Is it possible to alter the above code to get just the blood volume?

You can enable the option that computes statistics for visible segments only, and can (temporarily) hide the segment that you want to exclude from the computation.

If this is not good enough then you can add an API to Segment Statistics module so that it can take an optional Segment ID list. The module is a simple Python scripted module, so you can change it very easily. If you implement this then submit a pull request so that we can merge it into Slicer core.