Segment Editor - Repetitive function

@manjula I think I have figured out why it overwrites the other segment. If you change the ‘Modify other segments’ setting to ‘Allow overlap’ in the masking settings area down the bottom of segment editor then the other segment is not overwritten.

It took me a while to figure out how to change this setting in python but I can across this post which describes how to change the masking settings. So I have added the line segmentEditorNode.SetOverwriteMode(2) which I think changes the ‘Modify other segments’ setting to ‘Allow overlap’.

It worked on my computer. Hopefully it is ok for you. Updated code below.

growSize = 0.5 # set how much you want to grow the segment per iteration here
nOfIterations = 4 # set how many times you want to run the effect here

volume = getNode('vtkMRMLScalarVolumeNode1') # put the name of the main volume here

# get the segmentation node
segmentationNode = getNode('vtkMRMLSegmentationNode1') # put the name of the segment node here
segmentationNode.CreateDefaultDisplayNodes()
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(volume)
segmentID = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName('Segment_1') # put the name of the segment here


# setup temporary segment editor widget
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(volume)
segmentEditorNode.SetSelectedSegmentID(segmentID)
segmentEditorNode.SetOverwriteMode(2)

# set active effect to 'Margin' and set margin size
segmentEditorWidget.setActiveEffectByName("Margin")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MarginSizeMm", str(growSize))

totalGrowth = 0

# run the effect several times using a loop
for i in range(nOfIterations):
  effect.self().onApply()
  totalGrowth += growSize

# delete temporary segment editor
segmentEditorWidget = None
slicer.mrmlScene.RemoveNode(segmentEditorNode)

print('The Segment Has Successfully Grown by ' + str(totalGrowth) + 'mm')
#
1 Like