# Get labelmap from each segment separately?

**URL:** <https://discourse.slicer.org/t/get-labelmap-from-each-segment-separately/13231>\
**Category:** Support\
**Created:** [August 29, 2020, 10:57am UTC](https://discourse.slicer.org/t/get-labelmap-from-each-segment-separately/13231 "2020-08-29T10:57:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![44REAM](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/44ream/32/7697_2.png) [@44REAM](https://discourse.slicer.org/u/44REAM)\
**Post date:** [August 29, 2020, 10:57am UTC](https://discourse.slicer.org/t/get-labelmap-from-each-segment-separately/13231/1 "2020-08-29T10:57:32Z")

</div>

![label](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/4/54a105a5d38492dfa6aac90791e556a750dc9ab7.png)

I try to convert segmentation to labelmap separately from cli (eg labelmap of body, lung\_L and lung\_R separately). I found that this code below can export visable segment to labelmap.

```
seg = getNode(‘Segmentation’)
reference = getNode (‘InputVolume’) # this will be the volume the segmentation was drawn on
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLLabelMapVolumeNode’)

ids = vtk.vtkStringArray()
seg.GetDisplayNode().GetVisibleSegmentIDs(ids)
slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(seg, ids, labelmapVolumeNode, reference)

```

But how can I turn visible on or off from cli, or there other method to export labelmap separately from cli?.

---

<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:** [August 29, 2020, 2:11pm UTC](https://discourse.slicer.org/t/get-labelmap-from-each-segment-separately/13231/2 "2020-08-29T14:11:56Z")

</div>

You can change visibility of a segment by using [`SetSegmentVisibility`](https://github.com/Slicer/Slicer/blob/master/Libs/MRML/Core/vtkMRMLSegmentationDisplayNode.h#L205) method of the segmentation display node.

However, it is probably easier and faster to get all segments at once, as a numpy array, and then use standard numpy operations to extract/recolor a chosen all voxel that have a particular value (see for [this example](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Mask_volume_using_segmentation) to mask a scalar volume - you can mask a labelmap volume the same way).

---

<div class="post-metadata">

**Author:** ![mangotee](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mangotee](https://discourse.slicer.org/u/mangotee)\
**Post date:** [August 31, 2020, 10:49am UTC](https://discourse.slicer.org/t/get-labelmap-from-each-segment-separately/13231/3 "2020-08-31T10:49:35Z")

</div>

There is another simple way to accomplish this with an in-built function from `vtkSlicerSegmentationsModuleLogic`, which I’ve been using recently, and which circumvents segment visibilites entirely. Example code is below - if you need to convert regions one-by-one and separately, use lists of length 1 (e.g. [“background”], [“prostate”], [“tumor”]) instead of a list with multiple regions ([“background”, “prostate”, “tumor”]):

```
# nseg ... segmentation node
# nvol ... volume reference node of the segmentation 
# nlab ... node of the target labelmap (will adopt the geometry of nvol)
# listSegmentNames ... a list of strings with segment names to be included
# in the labelmap (for single-region: a string list of len 1)

# Convert list of segment names to a vtkStringArray of segment IDs
strarr = vtk.vtkStringArray()
for sname in listSegmentNames:
    sid = nseg.GetSegmentation().GetSegmentIdBySegmentName(sname)
    strarr.InsertNextValue(sid)
# Export the list of segments to a labelmap
slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsToLabelmapNode(nseg, strarr, nlab, nvol)
```
