mask图像与labelme中json格式文件相互转化

做图像分割或者图像检测时,需要标注图像,使用labelme进行标注的时候,做图像分割时需要生成mask图像,做图像检测时需要这些标注区域的轮廓点坐标。通常,标注直接生成其中一种文件,即只生成json文件或只生成Mask图像。故在此贴出mask图像与json文件相互转换的Python代码。

labelme的json转mask图

import cv2
import json
import numpy as np
import os
def func(file):
    with open(file, mode='r', encoding="utf-8") as f:
        configs = json.load(f)
    shapes = configs["shapes"]

    png = np.zeros((configs["imageHeight"], configs["imageWidth"],3), np.uint8)

    for shape in shapes:
        contours = [[point] for point in shape["points"]]
        if contours is not None and len(contours) > 0:
            cv2.drawContours(png, [np.array(contours, np.int32)], -1,(255, 255, 255),-1)
    return png


if __name__ == "__main__":

    save_mask_path = r'' #保存mask图路径
    json_path = r''  #读取json路径

    if os.path.isdir(json_path):
        for file in os.listdir(json_path):
            cv2.imwrite(os.path.join(save_mask_path, os.path.splitext(file)[0] + ".png"),
                        func(os.path.join(json_path, file)))

mask图转json

import cv2
import os
import json


def func(file):
    png = cv2.imread(file)
    gray = cv2.cvtColor(png, cv2.COLOR_BGR2GRAY)
    contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    dic = {"version": "5.0.1", "flags": {}, "shapes": list(), "imagePath": os.path.basename(file),
           "imageHeight": png.shape[0], "imageWidth": png.shape[1]}
    for contour in contours:
        temp = list()
        for point in contour[2:]:
            temp.append([int(point[0][0]), int(point[0][1])])
        dic["shapes"].append({"label": "cell", "points": temp, "group_id": None,
                              "shape_type": "polygon", "flags": {}})

    return dic


if __name__ == "__main__":

    mask_path = r' ' #mask图的路径
    save_json_path = r'' #保存json路径:
        for file in os.listdir(mask_path):
            with open(os.path.join(save_json_path, os.path.splitext(file)[0] + ".json"), mode='w', encoding="utf-8") as f:
                json.dump(func(os.path.join(mask_path, file)), f)

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值