Change color of segmentation node

How could I control the color of a ‘vtkMRMLSegmentationNode’?

Documentation for the segmentation module can be found here: https://www.slicer.org/wiki/Documentation/4.6/Modules/Segmentations

For colour, take a look at the figure on the right of the two figures under ‘Use Cases’. Double clicking the squares at the left of the table brings up an interface to change the segmentation’s colour.

Thanks for the reply! To be more specific, I wanted to control this through code.

Some code I use to do that:

import vtkSegmentationCorePython  # http://na-mic.org/Mantis/view.php?id=4271
segmentation = segmentationNode.GetSegmentation()
stringArray = vtk.vtkStringArray()
segmentation.GetSegmentIDs(stringArray)
displayNode = segmentationNode.GetDisplayNode()
colorVector = vtk.vtkVector3d()
for i in range(stringArray.GetNumberOfValues()):
    segmentID = stringArray.GetValue(i)
    segment = segmentation.GetSegment(segmentID)
    name = segment.GetName()
    color = (100, 150, 200)
    color = np.array(color, float) / 255
    try:
        segment.SetColor(color)
    except AttributeError:  # older versions of Slicer
        colorVector.Set(*color)
        displayNode.SetSegmentColor(segmentID, colorVector)

(I don’t know why the code formatting is not working). [I added the fix for the code formatting -Steve]

1 Like

@pieper thanks for the edit :+1:

@jks1995 if my answer helped you, you can mark it with “This reply solves my problem” so that the next person that comes can quickly find the solution.

Several convenience functions have been added recently to vtkMRMLSegmentation node, which should make it really simple to perform commonly needed operations.

For example, to add a new segment with a specific color, use vtkMRMLSegmentationNode::AddSegment... methods:

segmentID = segmentationNode.AddSegmentFromBinaryLabelmapRepresentation (imageData,"my segment name", [1,0,0.5])

If you later need to change any property then use the returned segmentID:
segmentationNode.GetSegment(segmentID).SetColor(0.3, 0.3, 0.5)

Let me know if there is any commonly needed operation that is cumbersome to do using the current segmentation API.

Thanks for the reply. I tried using this, but the AddSegmentFromBinaryLabelmapRepresentation method gave an error saying it needed an oriented label map instead of a label map. whats the best approach for taking a label map to an oriented label map?

What is your input?

vtkImageData => Create a vtkOrientedImageData object and ShallowCopy the content of your vtkImageData object. vtkOrientedImageData allows you to specify arbitrary axis directions.

vktMRMLLabelMapNode => Use vtkSlicerSegmentationsModuleLogic’s CreateSegmentFromLabelmapVolumeNode or ImportLabelmapToSegmentationNode methods.

sl = slicer.modules.segmentations.logic()
sl.ImportLabelmapToSegmentationNode(labelmapNode, segmentationNode)

I haven been able to create the segmentation using the segmentations logic, but unable to access any color aspects of the node at that point. So, I have done
slicer.modules.segmentations.logic()
sl.ImportLabelmapToSegmentationNode(labelmapNode, segmentationNode)

But if i try to run:
segmentationNode.GetDisplayNode().SetSegmentColor(segmentID, [0.3, 0.3, 0.5])

I get an error there is no attribute SetSegmentColor.

Sorry, I was looking at an older API. Color is actually stored in the segment:

import vtkSegmentationCorePython as vtkSegmentationCore
segmentationNode = getNode('Segmentation')
segmentation = segmentationNode.GetSegmentation()
segment = segmentation.GetSegment(segmentation.GetNthSegmentID(0))
segment.SetColor(0,0,1)
1 Like

Thank you for the replies!

The segmentation node just gives me a vtkObject when I call GetSegment, though.

Run import vtkSegmentationCorePython as vtkSegmentationCore to get access to segmentation objects (we’ll probably import this module by default in the future)

2 Likes