How to rename segments in a segmentation node?

Operating system: Windows 10
Slicer version:4.11
How can I change the names of segments in a segmentation. I can do this manually in the GUI but I want to know if there is any way to do this programmatically. I can’t see any mrml node ids for each individual segment.

There are no nodes for individual segments. The segmentation node has a vtkSegmentation object which holds all of the segments.

You can get a segmentation from the segmentation node, and change the name like this:

segmentation = segmentationNode.GetSegmentation()
segment = segmentation.GetNthSegment(0)
segment.SetName("NewName")

vtkSegmentation doc: Slicer: vtkSegmentation Class Reference
vtkSegment doc: Slicer: vtkSegment Class Reference

3 Likes

@Sunderlandkyl Hey, thanks. Could you also tell me if there is a way to setname for vtkSegmentation and not just vtkSegment?

If you are trying to change the name of the entire segmentation node, you can use:

segmentationNode.SetName("NewName")
2 Likes

@Sunderlandkyl thanks again. Sorry for asking again and again but Similarly, if I have other types of nodes like markupcurvesnode but not one but multiple and want to change there names , how would I access each to set names?

The name of every node can be changed with the SetName method.

1 Like