Merging Multiple Segmentations into a single LabelMap

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

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

I will not be able to call this

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

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

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

What do i do?

You could convert the overlayed_img RGB image to grayscale before applying another outline.

1 Like

Hello @msrepo,

Code below shows how to do this up to the alpha blending step. Please see this jupyter notebook for the alpha blending with mask code.

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)

color_contours = sitk.LabelToRGB(combined_contours)

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

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

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.