# Get label map node from segmentation node

**URL:** <https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137>\
**Category:** Development\
**Tags:** segmentation\
**Created:** [September 27, 2017, 7:49pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137 "2017-09-27T19:49:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![snvl](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@snvl](https://discourse.slicer.org/u/snvl)\
**Post date:** [September 27, 2017, 7:49pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/1 "2017-09-27T19:49:30Z")

</div>

Hello!

Is there a way to create a label map node from a segmentation node? I know I can go the other way (from a label map to a segmentation), but I can’t seem to find a way to do the opposite. If this is not possible is there a way to at least get the array underlying the segmentation node? I know I can get an oriented image data, but if I do slicer.util.array on the oriented image data, nothing is returned.

Thanks!

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [September 27, 2017, 8:15pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/2 "2017-09-27T20:15:14Z")

</div>

Answered in

> [@Export volume data in segment?](https://discourse.slicer.org/t/export-volume-data-in-segment/603):
>
> Is it possible to export the volume data in a segment? Meaning, using the Segment Editor, you create a segment, then export a TIFF stack or NRRD with the same volume size as your original data set, but with just the data in your segment, all other voxels at 0? Thanks! -Hollister

You can also reach this function (i.e. Segmentations module import/export) from the Data module (right-click the segmentation), and in Segment Editor as an option for the Segmentations button.

---

<div class="post-metadata">

**Author:** ![snvl](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@snvl](https://discourse.slicer.org/u/snvl)\
**Post date:** [September 27, 2017, 8:20pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/3 "2017-09-27T20:20:10Z")

</div>

Awesome! Do you know how I might do this through the python code?

Found it:  
`
slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode()
`

---

<div class="post-metadata">

**Author:** ![snvl](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@snvl](https://discourse.slicer.org/u/snvl)\
**Post date:** [September 27, 2017, 8:31pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/4 "2017-09-27T20:31:07Z")

</div>

Do you know what the inputs for this function is or where the documentation is for the segments?

slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode()

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [September 27, 2017, 8:35pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/5 "2017-09-27T20:35:24Z")

</div>

These are the available signatures

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Segmentations/Logic/vtkSlicerSegmentationsModuleLogic.h#L166-L192>

A few examples of usage

> **[Slicer/Slicer](https://github.com/Slicer/Slicer/search?utf8=%E2%9C%93&q=ExportSegmentsToLabelmapNode&type=)**
>
> Multi-platform, free open source software for visualization and image computing. - Slicer/Slicer

---

<div class="post-metadata">

**Author:** ![snvl](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@snvl](https://discourse.slicer.org/u/snvl)\
**Post date:** [September 27, 2017, 8:38pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/6 "2017-09-27T20:38:34Z")

</div>

Are there examples in python?

---

<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:** [September 27, 2017, 8:39pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/7 "2017-09-27T20:39:44Z")

</div>

You can always use help() function in Python to get documentation, for example:  
help(slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode)

Example where it is used: [https://github.com/Slicer/Slicer/blob/master/Utilities/Templates/Modules/ScriptedSegmentEditorEffect/SegmentEditorTemplateKeyLib/SegmentEditorEffect.py#L97](https://github.com/Slicer/Slicer/blob/master/Utilities/Templates/Modules/ScriptedSegmentEditorEffect/SegmentEditorTemplateKeyLib/SegmentEditorEffect.py#L97)

To create a custom Segment Editor effect, use ExtensionWizard module and choose ScriptedSegmentEditorEffect module template. It creates a fully functional effect that uses SimpleITK and you just have to replace the core of it to do what you need.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [September 28, 2017, 7:01am UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/8 "2017-09-28T07:01:58Z")

</div>

@snvl The first two results in the search that was the second link in my post above are python examples.

---

<div class="post-metadata">

**Author:** ![aiden.zhu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aiden.zhu/32/67145_2.png) [@aiden.zhu](https://discourse.slicer.org/u/aiden.zhu)\
**Post date:** [August 29, 2019, 7:37pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/10 "2019-08-29T19:37:38Z")

</div>

@cpinter Would you please help explain this to me?  
inside here:  
slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsToLabelmapNode(segmentationNode, segs, tmpLabelmapNode, masterVolumeNode)  
where segs has to be an vtk.vtkStringArray() and the segs may be obtained by:

segmentationNode = self.scriptedEffect.parameterSetNode().GetSegmentationNode()  
segs = vtk.vtkStringArray()  
segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(segs)

But while I try to get the selected segment-ID through:

segmentationNode = self.scriptedEffect.parameterSetNode().GetSegmentationNode()  
selectedSegmentID = self.scriptedEffect.parameterSetNode().GetSelectedSegmentID()  
where I just got a simple string for selectedSegmentID

How can I get a vtk.vtkStringArray for this selectedSegmentID or any easy conversion?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [August 29, 2019, 7:51pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/11 "2019-08-29T19:51:34Z")

</div>

```
sa = vtk.vtkStringArray()
sa.InsertNextValue(selectedSegmentID)

```

FYI the documentation tells you everything  
[https://vtk.org/doc/nightly/html/classvtkStringArray.html](https://vtk.org/doc/nightly/html/classvtkStringArray.html)

---

<div class="post-metadata">

**Author:** ![aiden.zhu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aiden.zhu/32/67145_2.png) [@aiden.zhu](https://discourse.slicer.org/u/aiden.zhu)\
**Post date:** [August 29, 2019, 8:13pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/12 "2019-08-29T20:13:49Z")

</div>

@cpinter Thank you so much for the quick reply. Sorry I am kind of new to vtk.  
👍  
Best,  
Aiden

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [November 22, 2019, 2:48pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/13 "2019-11-22T14:48:01Z")

</div>

@lassoan @cpinter is it possible to get the reference volume?

I searched, but could not find the answer. I was confused by the comment for the method below, it just returns a constant string.

> <https://github.com/Slicer/Slicer/blob/master/Libs/MRML/Core/vtkMRMLSegmentationNode.h#L181-L182>

---

<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:** [November 22, 2019, 3:05pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/14 "2019-11-22T15:05:35Z")

</div>

The constant string is the name of the node reference role. You can get the reference volume node like this: `segmentationNode.GetNodeReference(slicer.vtkMRMLSegmentationNode.GetReferenceImageGeometryReferenceRole()) `

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [November 23, 2019, 1:08am UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/15 "2019-11-23T01:08:19Z")

</div>

Thank you Andras, this resolved the latest issue I had!

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [November 26, 2019, 11:43pm UTC](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137/16 "2019-11-26T23:43:21Z")

</div>

Related to this issue, I wanted to mention that I used this approach without reading documentation (my usual mode of operation 🙄) as follows:

```python
segmentLabelmapNode = slicer.vtkMRMLLabelMapVolumeNode()
slicer.mrmlScene.AddNode(segmentLabelmapNode)
segmentIDs = vtk.vtkStringArray()
segmentIDs.InsertNextValue(0)
referenceNode = segmentationNode.GetNodeReference(slicer.vtkMRMLSegmentationNode.GetReferenceImageGeometryReferenceRole())
slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(segmentationNode, segmentIDs, segmentLabelmapNode, referenceNode)

```

Somehow this code works in the nightly, but when I do this in 4.10.2 I get error (`InsertNextValue` expects a string). If I do pass 0 as a string, I get a crash. The correct way to initialize that array, as I understand it, is the following:

```python
segmentationNode.GetSegmentation().GetSegmentIDs(segmentIDs)

```

Just in case this can help someone else.
