Hi,
I have written a script to apply thresholds to a segment and then to apply median smoothing. Both these actions work exactly as expected separately, but when chained together in the script only the first (thresholding) is applied. Anyone able to help with this?
# Create new segments in Segmentation
segmentationNode = getNode('Segmentation')
SingleSegment = segmentationNode.GetSegmentation().AddEmptySegment('Single')
MultiSegment = segmentationNode.GetSegmentation().AddEmptySegment('Multi')
# OPTIONAL -- Set masterVolumeNode to first volume node
masterVolumeNode = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLScalarVolumeNode')
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
# OPTIONAL -- Create temporary segment editor to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
# OPTIONAL -- Create segments by thresholding
### This will show available effects and parameters: print(slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLSegmentEditorNode"))
segmentsFromHounsfieldUnits = [
["Mask", -200, -50] ]
for segmentName, thresholdMin, thresholdMax in segmentsFromHounsfieldUnits:
# Create segment
addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment(segmentName)
segmentEditorNode.SetSelectedSegmentID(addedSegmentID)
# Fill by thresholding
segmentEditorWidget.setActiveEffectByName("Threshold")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumThreshold",str(thresholdMin))
effect.setParameter("MaximumThreshold",str(thresholdMax))
effect.self().onApply()
# OPTIONAL -- Apply smoothing to threshold mask
smoothSegments = [
["Mask"] ]
maskSmoothID = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName("Mask")
for smoothSegment in smoothSegments:
segmentEditorNode.SetSelectedSegmentID(maskSmoothID)
segmentEditorWidget.setActiveEffectByName("Smoothing")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("KernelSizeMm",3)
effect.self().onApply()
# OPTIONAL -- Delete temporary segment editor
segmentEditorWidget = None
slicer.mrmlScene.RemoveNode(segmentEditorNode)