# Merging Multiple Segmentations into a single LabelMap

**URL:** https://discourse.itk.org/t/merging-multiple-segmentations-into-a-single-labelmap/4459
**Category:** Beginner Questions
**Tags:** labelmap, simpleitk
**Created:** [September 28, 2021, 6:01am UTC](https://discourse.itk.org/t/merging-multiple-segmentations-into-a-single-labelmap/4459 "2021-09-28T06:01:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![msrepo](https://discourse.itk.org/user_avatar/discourse.itk.org/msrepo/32/2307_2.png) [@msrepo](https://discourse.itk.org/u/msrepo)
#### Post date: [September 28, 2021, 6:01am UTC](https://discourse.itk.org/t/merging-multiple-segmentations-into-a-single-labelmap/4459/1 "2021-09-28T06:01:55Z")

</div>

Given multiple segmentations from various annotations in different image files, I would like to obtain contour overlays from each segmentations onto the image.

Although, I can overlay a single labelmap onto the image like so

```auto
overlayed_img = sitk.LabelMapContourOverlay(sitk.Cast(seg01,sitk.sitkLabelUInt8),img)

```

I will not be able to call this

```auto
multiple_overlay = sitk.LabelMapContourOverlay(sitk.Cast(seg02, sitk.sitkLabelUInt8), overlayed_img)

```

> sitk::ERROR: Pixel type: label of 8-bit unsigned integer is not supported in 2D byN3itk6simple33LabelMapContourOverlayImageFilterE

seems like Casting to float32 from 8bit UInt is not allowed

```auto

```

multiple\_overlay = sitk.LabelMapContourOverlay(sitk.Cast(seg02, sitk.sitkLabelUInt8), sitk.Cast(overlayed\_img,sitk.sitkFloat32))

```auto

 sitk::ERROR: Filter does not support casting from casting vector of 8-bit unsigned integer to 32-bit float

What do i do?
```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [September 28, 2021, 1:31pm UTC](https://discourse.itk.org/t/merging-multiple-segmentations-into-a-single-labelmap/4459/2 "2021-09-28T13:31:59Z")

</div>

You could [convert](https://itk.org/ITKExamples/src/Filtering/ImageIntensity/ConvertRGBImageToGrayscaleImage/Documentation.html) the `overlayed_img` RGB image to grayscale before applying another outline.

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [September 28, 2021, 3:50pm UTC](https://discourse.itk.org/t/merging-multiple-segmentations-into-a-single-labelmap/4459/3 "2021-09-28T15:50:33Z")

</div>

Hello @msrepo,

Code below shows how to do this up to the alpha blending step. Please see [this jupyter notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/05_Results_Visualization.ipynb) for the alpha blending with mask code.

```auto
import SimpleITK as sitk
import numpy as np

seg1 = sitk.Image([128,128], sitk.sitkUInt8)
seg1[20:30,20:40] = 2
seg1[40:70,40:50] = 1
seg2 = sitk.Resample(seg1, sitk.TranslationTransform(2,[5,10]), sitk.sitkNearestNeighbor,0)
segmentations = [seg1, seg2]

segmentation_contours = [sitk.LabelContour(seg, fullyConnected=True, backgroundValue=0) for seg in segmentations]

# Combine all contours into one image. When there is overlap, the last image label is what is
# used.
combined_contours_array = sitk.GetArrayFromImage(segmentation_contours[0])
for contours in segmentation_contours[1:]:
    arr_view = sitk.GetArrayViewFromImage(contours)
    combined_contours_array[arr_view!=0] = arr_view[arr_view!=0]
combined_contours = sitk.GetImageFromArray(combined_contours_array)
combined_contours.CopyInformation(segmentations[0])

color_contours = sitk.LabelToRGB(combined_contours)
sitk.Show(color_contours)

# Combine the color_contours image with the original using alpha blending with a mask, see jupyter notebook.

```

---

<div class="post-metadata">

### Author: ![msrepo](https://discourse.itk.org/user_avatar/discourse.itk.org/msrepo/32/2307_2.png) [@msrepo](https://discourse.itk.org/u/msrepo)
#### Post date: [October 1, 2021, 8:13am UTC](https://discourse.itk.org/t/merging-multiple-segmentations-into-a-single-labelmap/4459/4 "2021-10-01T08:13:13Z")

</div>

wow. Thank you @zivy . You guys are really very active community.

---

<div class="post-metadata">

### Author: ![msrepo](https://discourse.itk.org/user_avatar/discourse.itk.org/msrepo/32/2307_2.png) [@msrepo](https://discourse.itk.org/u/msrepo)
#### Post date: [October 1, 2021, 8:14am UTC](https://discourse.itk.org/t/merging-multiple-segmentations-into-a-single-labelmap/4459/5 "2021-10-01T08:14:19Z")

</div>

Thank you @dzenanz . I ended up bringing the array to numpy and working there and back to sitk, but let me try what you suggested too.
