将标注的线段变为mask

本文介绍如何使用OpenCV的cv2.polylines()函数为JSON标注文件中的多类别形状生成掩码,包括nzm和xgb类别,并演示了不同类别对应的线条绘制。通过实例展示了如何读取json数据,解析标签,并根据类别绘制不同颜色的多边形。
摘要由CSDN通过智能技术生成

cv2.polylines()函数的具体参数见博客

import cv2
import numpy as np
import json
import os
import glob
seq_dir = os.path.expanduser('./')
img_files=glob.glob(seq_dir + '*.json') 
print(img_files)
category_types = [ "nzm", "xgb"]
for img_file in img_files:
    imgn=str(img_file[2:7])+".jpg"
    print(str(imgn))
    img = cv2.imread(str(imgn))
    h, w = img.shape[:2]
    mask = np.zeros([h, w, 1], np.uint8)    # 创建一个大小和原图相同的空白图像

    with open(img_file, "r") 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))
        if category == "nzm":
        	# 调试时将Tom的填充颜色改为255,便于查看
            mask = cv2.polylines(mask, [points_array], False, 255, 3)
        elif category == "xgb":
            mask = cv2.polylines(mask, [points_array], False, 25, 3)
        else:
            mask = cv2.polylines(mask, [points_array], False, 0, 3)

    cv2.imwrite("mask/"+str(img_file[2:7])+'.jpg', mask)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值