I was performing logical operations on some segmentations programmatically, when I stumbled across a bizarre occurrence: when performing intersection, I had to rerun my intersection function (with the exact same parameters) to completely perform the intersection–running it once resulted in a partially intersected output. I checked using the GUI’s segment editor, and it was able to perform the intersection in one go. I am wondering if there is a parameter I need to change in my intersection and/or setup functions.
For reference, I will paste my setup and intersection code below, along with the script I ran to get this strange output:
def setupSegmentEditor():
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
slicer.mrmlScene.AddNode(segmentEditorNode)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
workHorse = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode", "workHorse") # segmentation node that will do all processing
masterVolumeNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode") # background volume (irrelevant, but necessary to define)
assert(masterVolumeNode is not None)
segmentEditorWidget.setSegmentationNode(workHorse)
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
segmentEditorWidget.setActiveEffectByName('Logical operators')
segmentationLogic = slicer.modules.segmentations.logic()
segmentEditorNode.SetMaskMode(workHorse.EditAllowedInsideAllSegments)
segmentEditorNode.SetOverwriteMode(segmentEditorNode.OverwriteAllSegments)
return segmentEditorWidget, segmentEditorNode, workHorse, masterVolumeNode, segmentationLogic
def intersectSegments(segmentEditorNode, segmentEditorWidget, baseSegmentID, toIntersectSegmentID):
segmentEditorNode.SetSelectedSegmentID(baseSegmentID)
segmentEditorWidget.setActiveEffectByName('Logical operators')
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation", SegmentEditorEffects.LOGICAL_INTERSECT)
effect.setParameter("BypassMasking",1)
effect.setParameter("ModifierSegmentID",toIntersectSegmentID)
effect.self().onApply()
# script
segmentEditorWidget, segmentEditorNode, workHorse, masterVolumeNode, segmentationLogic = setupSegmentEditor()
# works
intersectSegments(segmentEditorNode, segmentEditorWidget, baseSegmentID, toIntersectSegmentID)
intersectSegments(segmentEditorNode, segmentEditorWidget, baseSegmentID, toIntersectSegmentID)
# Does not work
intersectSegments(segmentEditorNode, segmentEditorWidget, baseSegmentID, toIntersectSegmentID)
If it would help, I would be happy to provide the segmentationNode and segmentIDs I was using. Thanks!