简体   繁体   English

如何堆叠多个二进制掩码以创建用于多类分割的单个掩码?

[英]How to stack multiple binary masks to create a single mask for multiclass segmentation?

So I have 5 classes for segmentation (excluding background) and all the masks are binary masks in different folders.所以我有5个分割类(不包括背景),所有的掩码都是不同文件夹中的二进制掩码。 How do I combine the binary masks to create a single mask for all classes for multiclass segmentation?如何组合二进制掩码为所有类创建单个掩码以进行多类分割?

I tried by using 2 classes in different color channels, but since there are only 3 color channels I could only incorporate the background and 2 classes in a single mask.我尝试在不同的颜色通道中使用 2 个类,但由于只有 3 个颜色通道,我只能将背景和 2 个类合并到一个蒙版中。

python code: python 代码:

new_img = np.zeros((height, width,3))

new_img[:,:,0] = new_img[:,:,0] + class1[:,:,0]

new_img[:,:,1] = new_img[:,:,1] + class2[:,:,0]

Is there any other way to do this?有没有其他方法可以做到这一点? and could I use the binary masks for multi-class segmentation without combining them?我可以在不组合它们的情况下使用二进制掩码进行多类分割吗?

Thanks谢谢

You can combine the masks by making a mask with 6 channels:您可以通过制作具有 6 个通道的蒙版来组合蒙版:

mask = np.stack([mask0, mask1, mask2, mask3, mask4, mask5], axis=-1)

Assuming each maski for i in 0..5 is a 2d array.假设maskii的每个0..5是一个二维数组。

If you want to visualize the image, you have to map each class to a color.如果要可视化图像,则必须将 map 每个 class 转换为一种颜色。 For example:例如:

@np.vectorize(signature='(6)->(3)')
def mapcolors(x):
    i = np.argmax(x)
    if i == 0:
        return [0.,0.,0.]
    elif i == 1:
        return [0.,0.,1.]
    elif i == 2:
        return [0.,1.,0.]
    elif i == 3:
        return [1.,0.,0.]
    elif i == 4:
        return [0.,1.,1.]
    else:
        return [1.,1.,0.]

image = mapcolors(mask)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM