Carla语义分割图数据的进阶处理

详细代码可见Carla的manual_control.py中的语义分割传感器设置。这里只做核心部分的讲解。链接:https://github.com/carla-simulator/carla/blob/master/PythonAPI/examples/manual_control.py#L1042

一、设置语义分割传感器

['sensor.camera.semantic_segmentation', cc.Raw, 'Camera Semantic Segmentation (Raw)', {}],
['sensor.camera.semantic_segmentation', cc.CityScapesPalette,
                'Camera Semantic Segmentation (CityScapes Palette)', {}],
  • 注意这里用的是cc.Raw类型,也就是原始数据。
  • 另一种类型是cc.CityScapesPalette,将城市类型的标签上色后存储的图片。类似于下图:
    在这里插入图片描述

二、语义分割的标签

三、如何设置我想要的语义分割图呢?

3.1 摄像头传感器处理raw_data

    array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
    array = np.reshape(array, (image.height, image.width, 4))
    array = array[:, :, :3]
    array = array[:, :, ::-1]
    if self.recording:
    	image.save_to_disk('%08d' % image.frame)
  • 第一步,将这个流数据转换为设置摄像头时候的图片大小
  • 第二步,将[h,w,4]转换为[h,w,3]
  • 第三步,如果选择存储的话,那么保存的是image,大小为[h,w,4]

3.2 处理语义分割图的cc.Raw

  • 在文档中,标签存储在了Red通道,也就是第0个通道,存着对应的标签,因此先定义第一个函数,找到标签。
def process_image(image):
    # [480,640,4] --> [480,640,3]
    image = image[:, :, :3]

    # [0,1] --> [0,255]
    image = image * 255

    # Get the r channel
    sem = image[:, :, 0]

    return sem
  • 设置语义分割的字典,label-->color
SEM_COLORS = {

    4: (220, 20, 60),
    6: (157, 234, 50),
    7: (128, 64, 128),
    10: (0, 0, 142),

}

  • 将对应位置的标签上色
def visualize_semantic(sem, labels=[4, 6, 7, 10]):
    canvas = np.zeros(sem.shape + (3,), dtype=np.uint8)
    # print("shape of canvas:",canvas.shape)
    for label in labels:
        # print(label)
        canvas[sem == label] = SEM_COLORS[label]

    return canvas

3.3 最终效果

    # Add the file dircetory
    file_name = "EgoRgb/21057685.png"

    # Load the image
    image = plt.imread(file_name)

    print("shape of image", image.shape)
    
    # Get red channel labels
    sem = process_image(image)

    # Convert to the rgb sematic color
    vis_image = visualize_semantic(sem)

    plt.imshow(vis_image)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值