FCN图像分割 | 数据准备篇

参考:如何标注mask用于图像分割模型训练

制作自己的训练数据

1. 标记工具使用详情

使用VIA标注工具

  1. remove示例图片
    在这里插入图片描述

  2. add自己的图片
    在这里插入图片描述

  3. 根据任务不同设置属性
    纯分割一个物体不用管,如果分类多个物体请在另外查询。
    在这里插入图片描述

  4. 绘制轮廓
    在这里插入图片描述
    注意:在绘制中想要退出可以按esc键,结束绘制按enter键,单击选中轮廓可以再调节节点的位置。具体可以再参考Help-Gettong Started.
    在这里插入图片描述

  5. 保存
    save-ok在这里插入图片描述

2. 转换成mask

运行以下代码:

import os
import json
import numpy as np
import skimage.draw
import cv2

IMAGE_FOLDER = "./train/"
MASK_FOLOER = "./mask/"
PATH_ANNOTATION_JSON = 'box.json'

# 加载VIA导出的json文件
annotations = json.load(open(PATH_ANNOTATION_JSON, 'r'))
imgs = annotations["_via_img_metadata"]

for imgId in imgs:
    filename = imgs[imgId]['filename']
    regions = imgs[imgId]['regions']
    if len(regions) <= 0:
        continue

    # 取出第一个标注的类别,本例只标注了一个物件
    polygons = regions[0]['shape_attributes']

    # 图片路径
    image_path = os.path.join(IMAGE_FOLDER, filename)
    # 读出图片,目的是获取到宽高信息
    image = cv2.imread(image_path)  # image = skimage.io.imread(image_path)
    height, width = image.shape[:2]

    # 创建空的mask
    maskImage = np.zeros((height,width), dtype=np.uint8)
    countOfPoints = len(polygons['all_points_x'])
    points = [None] * countOfPoints
    for i in range(countOfPoints):
        x = int(polygons['all_points_x'][i])
        y = int(polygons['all_points_y'][i])
        points[i] = (x, y)

    contours = np.array(points)

    # 遍历图片所有坐标
    for i in range(width):
        for j in range(height):
            if cv2.pointPolygonTest(contours, (i, j), False) > 0:
                maskImage[j,i] = 1

    savePath = MASK_FOLOER + filename
    # 保存mask
    cv2.imwrite(savePath, maskImage)

猜你喜欢

转载自blog.csdn.net/weixin_42326479/article/details/107185379