Merge different labels in a single image

Hi, I have 7 different labels in each 7 different binary images. All of these images are 3D images provided by CT scan, they have a number of slices and 512x512 number of pixels for each slices. I want to merge these seven labels, that respectively represent the main chambers of the heart including aorta, in a single image having also different number of labels for each segmented area. Can someone can help me using sitk library?

Hello @EneaP,

Not sure why you labelled this question as “registration” as it isn’t what you are asking about, please remove that tag.

Solution below:

import SimpleITK as sitk

# Create binary label data
image_size = [512,512,30]
label1 = sitk.Image(image_size, sitk.sitkUInt8)
label1[50:100, 50:100, 10:25] = 1
label2 = sitk.Image(image_size, sitk.sitkUInt8)
label2[100:150, 100:150, 10:25] = 1
all_labels = [label1, label2]

# Combine binary label data into a single label mask
combined_labels = sitk.Image(all_labels[0].GetSize(), all_labels[0].GetPixelID())
for i, label in enumerate(all_labels, start=1):
    combined_labels += label*i
1 Like