# Segment ids when imported from label map

**URL:** <https://discourse.slicer.org/t/segment-ids-when-imported-from-label-map/12502>\
**Category:** Development\
**Tags:** segmentation, extensions-manager\
**Created:** [July 13, 2020, 11:40am UTC](https://discourse.slicer.org/t/segment-ids-when-imported-from-label-map/12502 "2020-07-13T11:40:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niels](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Niels](https://discourse.slicer.org/u/Niels)\
**Post date:** [July 13, 2020, 11:40am UTC](https://discourse.slicer.org/t/segment-ids-when-imported-from-label-map/12502/1 "2020-07-13T11:40:56Z")

</div>

I have a label map with numbers which I would like to keep when I convert it to a segment but I don’t know if this is possible.

For example i have labels 1,3,5,8 but I think I loose them to segment 0 1 2 and 4.

I have looked-up this function but I don’t know if this is what I need or how it works?

/// Update segmentation from segments in a labelmap node.  
/// \param updatedSegmentIDs Defines how label values 1…N are mapped to segment IDs (0…N-1).  
static bool ImportLabelmapToSegmentationNode(vtkOrientedImageData\* labelmapImage,  
vtkMRMLSegmentationNode\* segmentationNode, vtkStringArray\* updatedSegmentIDs,  
vtkGeneralTransform\* labelmapToSegmentationTransform=nullptr );

---

<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:** [July 14, 2020, 6:33am UTC](https://discourse.slicer.org/t/segment-ids-when-imported-from-label-map/12502/2 "2020-07-14T06:33:23Z")

</div>

Probably the easiest is to create a color table (.ctbl) file that contains your segment names. The format is a simple text file that you can create in any text editor:

```nohighlight
0 background 0 0 0 0
1 first 128 174 128 255
2 second 241 214 145 255
3 and_the_last_one 177 122 101 255

```

You can use this color table like this:

- Load this color table into Slicer
- Load the labelmap volume into Slicer, in Add data dialog check “Show options” and check “LabelMap” checkbox and select your custom color table
- Convert the loaded labelmap volume to segmentation volume by right-clicking on it in Data module

---

<div class="post-metadata">

**Author:** ![Niels](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Niels](https://discourse.slicer.org/u/Niels)\
**Post date:** [July 14, 2020, 7:50pm UTC](https://discourse.slicer.org/t/segment-ids-when-imported-from-label-map/12502/3 "2020-07-14T19:50:53Z")

</div>

Thanks lassoan!

But i still need to understand the ImportLabelmapToSegmentationNode function to get the right ids after import.

If found an alternative way by iterating the Label\_1 to Label\_N segments and i would like to change their id to somthing else but there is only a function to change a segment name. Not the id.

Do you know how this function works or how i change the ids?

I am working with python

---

<div class="post-metadata">

**Author:** ![Niels](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Niels](https://discourse.slicer.org/u/Niels)\
**Post date:** [July 15, 2020, 4:18pm UTC](https://discourse.slicer.org/t/segment-ids-when-imported-from-label-map/12502/4 "2020-07-15T16:18:22Z")

</div>

found a useful example here:

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Segmentations/Testing/Python/SegmentationsModuleTest1.py>
