语义分割 调色板代码

语义分割调色板代码

下面代码的主要作用:根据你的类别数量,生成相同数量的颜色。
比如你有21个类别(voc是20个class和1个background),就可生成相应21个不同的颜色。

def make_palette(num_classes):
    """
    Maps classes to colors in the style of PASCAL VOC.
    Close values are mapped to far colors for segmentation visualization.
    See http://host.robots.ox.ac.uk/pascal/VOC/voc2012/index.html#devkit


    Takes:
        num_classes: the number of classes 输入为类别数目
    Gives:
        palette: the colormap as a k x 3 array of RGB colors 输出为k×3大小的RGB颜色数组
    """
    palette = np.zeros((num_classes, 3), dtype=np.uint8)
    #xrange() 函数用法与 range 完全相同,所不同的是生成的不是一个数组,而是一个生成器
    for k in range(0, num_classes):
        label = k
        i = 0
        while label:  #按一定规则移位产生调色板
            palette[k, 0] |= (((label >> 0) & 1) << (7 - i)) #>>为二进制右移
            palette[k, 1] |= (((label >> 1) & 1) << (7 - i))
            palette[k, 2] |= (((label >> 2) & 1) << (7 - i))
            label >>= 3
            i += 1
    return palette

这几行代码的主要作用:根据语义分割得到的map和调色板生成最终的结果。seg是语义分割的结果,每个像素代表相应的类别,palette是上一个代码生成的调色板。

#产生一个可视化的类别数组
def color_seg(seg, palette):
    """
    Replace classes with their colors.

    Takes:
        seg: H x W segmentation image of class IDs 
        seg是score层产生的类别图(即每一像素的值是0-(num_classes-1)中的一个)
    Gives:
        H x W x 3 image of class colors
        生成一张三通道的彩色图(其中每一种颜色对应一种类别),实际生成是H×W×3的数组,需要经过python中但PIL库调用函数Image.fromarray将此数组转化为一张彩色图(详见infer.py最后几行)
    """
    return palette[seg.flat].reshape(seg.shape + (3,)) #按照类别进行上色

效果图如下
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值