# Change color of segmentation node

**URL:** <https://discourse.slicer.org/t/change-color-of-segmentation-node/442>\
**Category:** Support\
**Created:** [June 6, 2017, 2:40pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442 "2017-06-06T14:40:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jks1995](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@jks1995](https://discourse.slicer.org/u/jks1995)\
**Post date:** [June 6, 2017, 2:40pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/1 "2017-06-06T14:40:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![\_jmichael](https://avatars.discourse-cdn.com/v4/letter/_/ce7236/32.png) [@\_jmichael](https://discourse.slicer.org/u/_jmichael)\
**Post date:** [June 6, 2017, 2:55pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/2 "2017-06-06T14:55:10Z")

</div>

Documentation for the segmentation module can be found here: [https://www.slicer.org/wiki/Documentation/4.6/Modules/Segmentations](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.

---

<div class="post-metadata">

**Author:** ![jks1995](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@jks1995](https://discourse.slicer.org/u/jks1995)\
**Post date:** [June 6, 2017, 2:57pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/3 "2017-06-06T14:57:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [June 6, 2017, 3:13pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/4 "2017-06-06T15:13:42Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [June 6, 2017, 3:18pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/5 "2017-06-06T15:18:07Z")

</div>

@pieper thanks for the edit 👍

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [June 6, 2017, 3:20pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/6 "2017-06-06T15:20:21Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 6, 2017, 3:58pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/7 "2017-06-06T15:58:44Z")

</div>

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](http://apidocs.slicer.org/master/classvtkMRMLSegmentationNode.html#a840a6b209ad581c3947db5f4311082d2):

`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.

---

<div class="post-metadata">

**Author:** ![jks1995](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@jks1995](https://discourse.slicer.org/u/jks1995)\
**Post date:** [June 6, 2017, 8:47pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/8 "2017-06-06T20:47:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 6, 2017, 9:13pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/9 "2017-06-06T21:13:33Z")

</div>

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](http://apidocs.slicer.org/master/classvtkSlicerSegmentationsModuleLogic.html#a7800e526ccf33d4181fdb2e82a7f8623) or [ImportLabelmapToSegmentationNode](http://apidocs.slicer.org/master/classvtkSlicerSegmentationsModuleLogic.html#adf111c9bcd3ceb28d49028f6fc6a2506) methods.

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

---

<div class="post-metadata">

**Author:** ![jks1995](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@jks1995](https://discourse.slicer.org/u/jks1995)\
**Post date:** [June 7, 2017, 2:33pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/10 "2017-06-07T14:33:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 7, 2017, 3:21pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/12 "2017-06-07T15:21:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jks1995](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@jks1995](https://discourse.slicer.org/u/jks1995)\
**Post date:** [June 7, 2017, 3:37pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/13 "2017-06-07T15:37:39Z")

</div>

Thank you for the replies!

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

> [@](#):
>
> seg\_node = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLSegmentationNode’)  
> slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(out, seg\_node)  
> segmentation = seg\_node.GetSegmentation()  
> segment = segmentation.GetSegment(segmentation.GetNthSegmentID(0))  
> segment.SetColor(0,0,1)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 7, 2017, 3:39pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-node/442/14 "2017-06-07T15:39:32Z")

</div>

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