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
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)
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.