CityScapes数据集灰度标签图转为伪彩色图的python代码

import numpy as np
import cv2

ignore_label = 255
ID_TO_TRAINID = {-1: ignore_label, 0: ignore_label, 1: ignore_label, 2: ignore_label,
                    3: ignore_label, 4: ignore_label, 5: ignore_label, 6: ignore_label,
                    7: 0, 8: 1, 9: ignore_label, 10: ignore_label, 11: 2, 12: 3, 13: 4,
                    14: ignore_label, 15: ignore_label, 16: ignore_label, 17: 5,
                    18: ignore_label, 19: 6, 20: 7, 21: 8, 22: 9, 23: 10, 24: 11, 25: 12, 26: 13, 27: 14,
                    28: 15, 29: ignore_label, 30: ignore_label, 31: 16, 32: 17, 33: 18}

def label_to_color_image(label):
    # Color map
    color_map = np.array([
        [128,  64, 128], # road
        [244,  35, 232], # sidewalk
        [ 70,  70,  70], # building
        [102, 102, 156], # wall
        [190, 153, 153], # fence
        [153, 153, 153], # pole
        [250, 170,  30], # traffic light
        [220, 220,   0], # traffic sign
        [107, 142,  35], # vegetation
        [152, 251, 152], # terrain
        [ 70, 130, 180], # sky
        [220,  20,  60], # person
        [255,   0,   0], # rider
        [  0,   0, 142], # car
        [  0,   0,  70], # truck
        [  0,  60, 100], # bus
        [  0,  80, 100], # train
        [  0,   0, 230], # motorcycle
        [119,  11,  32]  # bicycle
    ])

    h, w = label.shape
    color_label = np.zeros((h, w, 3), dtype=np.uint8)

    for i in range(19):
        color_label[label == i] = color_map[i]

    return color_label

label = cv2.imread("aachen_000000_000019_gtFine_labelIds.png", cv2.IMREAD_GRAYSCALE)

# 原始标注34类转为19类
for k, v in ID_TO_TRAINID.items():
    label[label == k] = v

color_label = label_to_color_image(label)
color_label = cv2.cvtColor(color_label,cv2.COLOR_BGR2RGB)
cv2.imwrite("color_label.png", color_label)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值