0
点赞
收藏
分享

微信扫一扫

使用labelme制作多类别图像分割数据集


一、背景

近期制作了些关于毕业设计所使用的数据集,花费了大量精力。主要是用于多类别分割的。由于研究生阶段主要研究的重点不在此,故可能会有纰漏之处,望多指正!

二、方法

1、安装使用labelme进行标注图像,方法可以自行百度。下面是我标注的一个示例:

待标注图像:



标注示例:



其中标签:



使用labelme制作多类别图像分割数据集_Powered by 金山文档


  1. 将标注生成的json文件转换为mask用于分割训练

文件目录

— — — —

|— image

| |— 1.jpeg

| |— ...

|— json

| |— 1.json

| |— ...

|— masks

| |— 1.png

| |— ...

代码

import os
import cv2
import numpy as np
import json

'''
制作一个只包含分类标注的标签图像,假如我们分类的标签为cat和dog,那么该标签图像中,Background为0,cat为1,dog为2。
我们首先要创建一个和原图大小一致的空白图像,该图像所有像素都是0,这表示在该图像中所有的内容都是Background。
然后根据标签对应的区域使用与之对应的类别索引来填充该图像,也就是说,将cat对应的区域用1填充,dog对应的区域用2填充。
特别注意的是,一定要有Background这一项且一定要放在index为0的位置。
'''

# 分类标签,一定要包含'Background'且必须放在最前面
category_types = ['Background', 'mouth', 'ears', 'eyes', 'nose']
# 将图片标注json文件批量生成训练所需的标签图像png
imgpath_list = os.listdir(r'image')
for img_path in imgpath_list:
img_name = img_path.split('.')[0]
img = cv2.imread(os.path.join(r'image', img_path))
h, w = img.shape[:2]
# 创建一个大小和原图相同的空白图像
mask = np.zeros([h, w, 1], np.uint8)

with open(r'json/'+img_name+'.json', encoding='utf-8') as f:
label = json.load(f)

shapes = label['shapes']
for shape in shapes:
category = shape['label']
points = shape['points']
# 将图像标记填充至空白图像
points_array = np.array(points, dtype=np.int32)
mask = cv2.fillPoly(mask, [points_array], category_types.index(category))

# 生成的标注图像必须为png格式
cv2.imwrite(r'masks/'+img_name+'.png', mask)

三、打包示例文件和下载链接

链接:​​https://pan.baidu.com/s/1Y_8wvZ14OkDG0PBHTl7hjA?pwd=enlg​​

提取码:enlg

举报

相关推荐

0 条评论