Calculate Statistics with Python takes longer then with 3D Slicer GUI

thorbenjt
Mon, 18 Mar 2024 18:23:30 +0000
Support

Hi there,
i am currently working on a script the automates tresholding and in addition splitting the segments into islands.
this works just as fine as if i would do it per hand in the 3D Slicer GUI.
The critical part is the calculation of the Statistics. As stated in the title, it takes up to 2 mins to calculate the statistics when using the code, but only 17 seconds when using ther 3D Slicer GUI.

Here is the code which is based on code snippets i found in previous discussions:
node=slicer.util.loadVolume(filename)

segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode") # Creates Empty Segmentation
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(node)
addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("skin") #add segment

# Create segment editor to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget() #-> [Qt] QLayout::addChildLayout: layout "" already has a parent
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode) #->[Qt] ctkSliderWidget::setSingleStep()  0 is out of bounds. 0 100 1
segmentEditorWidget.setSourceVolumeNode(node)

# Thresholding
segmentEditorWidget.setActiveEffectByName("Threshold")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumThreshold","125.00")
effect.setParameter("MaximumThreshold","255.00")
effect.self().onApply()

segmentEditorWidget.setActiveEffectByName("Islands")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumSize","25")
effect.setParameter("Operation","SPLIT_ISLANDS_TO_SEGMENTS")
effect.self().onApply()
segmentEditorWidget.setActiveEffect(None)
segmentEditorWidget = None

segmentationNode  =  slicer.util.getNode('Segmentation')



resultsTableNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTableNode')


import SegmentStatistics
segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.enabled","True")
segStatLogic.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin","False")
segStatLogic.getParameterNode().SetParameter("CloseSurfaceStatisticsPlugin","False")
segStatLogic.computeStatistics()
stats = segStatLogic.getStatistics()

This is the file i want to segment:

Thanks in advance for any help

1 post - 1 participant