语义分割数据标注、格式转换

1. 数据准备

1.1 labelme标注数据

labelme标注完之后会生成json格式标注文件,与图像在同一文件夹下面,如图

1.2 labelme标注格式转换(json转voc格式)

labelme官方提供了标注格式转换脚本,https://github.com/wkentaro/labelme/tree/main/examples,其中包含了检测、分类、分割等标注任务的格式转换脚本,其中,语义分割的转换脚本位于https://github.com/wkentaro/labelme/tree/main/examples/semantic_segmentation,运行如下命令即可实现json格式转换为voc标准数据集格式,

python ./labelme2voc.py data_annotated data_dataset_voc --labels labels.txt

之后将生成如下文件,

 其中,SegmentationClassPNG为mask掩码标签,直接为png的8位伪彩色图。

注:

        1. voc的分割标注直接为png的8位伪彩色图,通过调用调色板来显示色彩。

        2. PIL中有九种不同模式。分别为1,L,P,RGB,RGBA,CMYK,YCbCr,I,F。其中,模式“P”为8位彩色图像,它的每个像素用8个bit表示,其对应的彩色值是按照调色板查询出来的。

python --- 之pil图像转换的一些方式_zxyhhjs2017的博客-CSDN博客_pil_image.fromarray

1.3 CVAT标注格式转换

image-20211116193255236

 

CVAT导出的标注mask并不是voc标准格式,是真彩色RGB图像,需要进行简单转换,转换代码如下:

import mmcv
import os.path as osp
from PIL import Image
import cv2
import numpy as np
import os

root = '/home/liuzhicai/project/mmsegmentation-0.17.0/data/bare_soil_voc'
masks = 'SegmentationClass_bak'

palette = [[0, 0, 0], [128, 0, 0], [0, 128, 0]]

save_masks = 'SegmentationClass'

if not osp.exists(osp.join(root, save_masks)):
    os.makedirs(osp.join(root, save_masks))

for file in mmcv.scandir(osp.join(root, masks), suffix='.png'):
    seg_map = cv2.imread(osp.join(root, masks, file), cv2.IMREAD_GRAYSCALE)
    seg_img = Image.fromarray(seg_map).convert('P')
    seg_img.putpalette(np.array(palette, dtype=np.uint8))
    seg_img.save(osp.join(root, save_masks, file))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值