How would I get the number of voxels off of a label map node?

lassoan
(Andras Lasso)
June 14, 2017, 3:43pm
#2
Use Labelmap statistics module to get the number of non-zero voxels.

1 Like

How would I go about that?

lassoan
(Andras Lasso)
June 15, 2017, 2:17am
#4
Open Label statistics module
Select Grayscale volume
Select Label map volume
Click Apply
`Count`

column contains the number of voxels.

How would I access this through python?

ihnorton
(Isaiah Norton)
June 15, 2017, 3:14pm
#6
The module is written in Python, so any specific functionality could be easily extracted/duplicated in your own script. Here is the relevant section:

# logic copied from slicer3 LabelStatistics
# to create the binary volume of the label
# //logic copied from slicer2 LabelStatistics MaskStat
# // create the binary volume of the label
thresholder = vtk.vtkImageThreshold()
thresholder.SetInputConnection(labelNode.GetImageDataConnection())
thresholder.SetInValue(1)
thresholder.SetOutValue(0)
thresholder.ReplaceOutOn()
thresholder.ThresholdBetween(i,i)
thresholder.SetOutputScalarType(grayscaleNode.GetImageData().GetScalarType())
thresholder.Update()
# this.InvokeEvent(vtkLabelStatisticsLogic::LabelStatsInnerLoop, (void*)"0.25");
# use vtk's statistics class with the binary labelmap as a stencil
stencil = vtk.vtkImageToImageStencil()
stencil.SetInputConnection(thresholder.GetOutputPort())
stencil.ThresholdBetween(1, 1)

This file has been truncated. show original

lassoan
(Andras Lasso)
June 15, 2017, 4:36pm
#7
If you want to build anything on this, then I would strongly recommend to switch to Segmentations/Segment Editor/Segment statistics modules now. These modules are much more capable than the old Editor/Label statistics - those old modules will be kept around for a while but they will not be developed any further.

See complete example of using Segment statistics in Python below. You can run the test by enabling developer mode in Slicer application settings, opening Segment statistics module, and clicking on `Reload and test`

button.

segment = segmentationNode.GetSegmentation().GetSegment(segmentID)
statistics = self.getStatistics()
if segmentID not in statistics["SegmentIDs"]:
statistics["SegmentIDs"].append(segmentID)
statistics[segmentID,"Segment"] = segment.GetName()
# apply all enabled plugins
for plugin in self.plugins:
pluginName = plugin.__class__.__name__
if self.getParameterNode().GetParameter(pluginName+'.enabled')=='True':
stats = plugin.computeStatistics(segmentID)
for key in stats:
statistics[segmentID,pluginName+'.'+key] = stats[key]
statistics["MeasurementInfo"][pluginName+'.'+key] = plugin.getMeasurementInfo(key)
def getPluginByKey(self, key):
"""Get plugin responsible for obtaining measurement value for given key"""
for plugin in self.plugins:
if plugin.toShortKey(key) in plugin.keys:
return plugin

This file has been truncated. show original

1 Like