Change Color of Segmentation by Script

Operating system: Window 7
Slicer version: 4.8.1

Hi,

I am trying to write a program to load two segmentation of Carotid done by two different people on the same volume, then I want to save images of all slices from axial view to a folder.

I could do most of the thing except changing the color of the segmentations. The reason is that I want to compare between two segmentations. It is hard to compare them when they are in the same color.

Is there anyway to write a script to change the color of the finished segmentation? I tried the below code, but it seems like it does not work this way:

segmentationNode = getNode(‘Segmentation’)
segmentationNode.SetColor(1,1,0)

or

segmentationDisplayNode = segmentationNode.GetDisplayNode()
segmentationDisplayNode.SetColor(1,1,0)

Thank you for all of your help.

You need to set color for individual segments. For example, set first segment color to red:

segmentation = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLSegmentationNode').GetSegmentation()
segmentation.GetNthSegment(0).SetColor(1,0,0)
2 Likes

Hi Andras,
I am setting color like this
scalpSeg = segmentationNode.GetSegmentation().GetSegment(scalpSelector)
scalpSeg.SetColor(255,192,203)
but its not working even if I change the color values. Not giving any error. code is working but cant visualize in the segment editor window

Thank you

Regards,
Saima Safdar

I believe setting segment color takes values from 0 to 1 where 255 is equivalent to 1. So setting to 255,192,203 is actually setting it to 1,1,1.

Hi,
If I have red =255
green = 192
and yellow = 203

so what will be the values in o to 1 range.

Regards,
Saima Safdar

You have to divide each of the colors by 255

1 Like