Copy Segment from Segmentation Failing

Hi,

Any help greatly appreciated. I am trying to copy a segment from one segmentation to another (Copy segment “Single” from segmentation “Segmentation” to new segmentation “Margin Expansion” to new segment “Margin + Expansion”). The below code seems to work perfectly until the final section and I get error “CopySegmentFromSegmentation: Failed to get segment!”

masterVolumeNode = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLScalarVolumeNode')

# Create segmentation
## Can adjust segmentation name in .AddNewNodeByClass 2nd parameter String
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode", "Margin Expansion")
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)

# 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)

## Create new segment with specified name
addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("Single + Margin")
segmentEditorNode.SetSelectedSegmentID(addedSegmentID)

## Copy "Single" segment "Segmentation" to new "Margin Expansion" segmentation

sourceSegmentationNode = getNode('Segmentation')
sourceSegmentation = getNode('Segmentation').GetSegmentation()
sourceSegmentId = "Single"

sourceSegmentationNode.GetSegmentation().CopySegmentFromSegmentation(sourceSegmentation, sourceSegmentId)
1 Like

I just realised what my error was. I was specifying the name of the segment when it required the ID. So I have adjusted to the following:

sourceSegmentationNode = getNode('Segmentation')
sourceSegmentation = getNode('Segmentation').GetSegmentation()
sourceSegmentId = sourceSegmentation.GetSegmentIdBySegmentName("Single")

sourceSegmentationNode.GetSegmentation().CopySegmentFromSegmentation(sourceSegmentation, sourceSegmentId)

However, the new segment is created in the original Segmentation, not the new one I just created (“Margin Expansion”). Is there a way to fix that?

Alternatively, I can keep it all in the same segmentation but need the ability to name the copied segment something original.

Many thanks in advance,

Pete

2 Likes

I seem to be able find the answers to all my own questions today with a lot of trial and error. In case anyone needs the solutions in the future:

sourceSegmentationNode = getNode('Segmentation')
sourceSegmentation = getNode('Segmentation').GetSegmentation()
sourceSegmentId = sourceSegmentation.GetSegmentIdBySegmentName("Single")

segmentationNode.GetSegmentation().CopySegmentFromSegmentation(sourceSegmentation, sourceSegmentId)

To rename a segment:

renamedSegmentId = getNode('Margin Expansion').GetSegmentation().GetSegmentIdBySegmentName("Single")
renamedSegment = getNode('Margin Expansion').GetSegmentation().GetSegment(renamedSegmentId)
renamedSegment.GetName()
renamedSegment.SetName('Test')
3 Likes