当前位置: 首页 > news >正文

【mask转json】文件互转

mask图像转json文件

当只有mask图像时,可使用下面代码得到json文件

import cv2
import os
import json
import sysdef func(file:str) -> dict:png = cv2.imread(file)gray = cv2.cvtColor(png, cv2.COLOR_BGR2GRAY)_, binary = cv2.threshold(gray,10,255,cv2.THRESH_BINARY)contours, _ = cv2.findContours(binary, 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:]:if len(temp) > 1 and temp[-2][0] * temp[-2][1] * int(point[0][0]) * int(point[0][1]) != 0 and (int(point[0][0]) - temp[-2][0]) * (temp[-1][1] - temp[-2][1]) == (int(point[0][1]) - temp[-2][1]) * (temp[-1][0] - temp[-1][0]):temp[-1][0] = int(point[0][0])temp[-1][1] = int(point[0][1])else:temp.append([int(point[0][0]), int(point[0][1])])dic["shapes"].append({"label": "result", "points":temp, "group_id": None,"shape_type": "polygon", "flags": {}})return dicif  __name__ == "__main__":if len(sys.argv) != 3:raise ValueError("mask文件或目录 输出路径")if os.path.isdir(sys.argv[1]):for file in os.listdir(sys.argv[1]):with open(os.path.join(sys.argv[2], os.path.splitext(file)[0]+".json"), mode='w', encoding="utf-8") as f:json.dump(func(os.path.join(sys.argv[1], file)), f)else:with open(os.path.join(sys.argv[2], os.path.splitext(os.path.basename(sys.argv[1]))[0]+".json"), mode='w', encoding="utf-8") as f:json.dump(func(sys.argv[1]), f)

json文件转mask图像

只有json文件时,可使用下面代码得到mask图像

import cv2
import json
import numpy as np
import os
import sysdef func(file:str) -> np.ndarray: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:cv2.fillPoly(png, [np.array(shape["points"], np.int32)], (0,0,255))return pngif  __name__ == "__main__":if len(sys.argv) != 3:raise ValueError("json文件或目录 输出路径")if os.path.isdir(sys.argv[1]):for file in os.listdir(sys.argv[1]):cv2.imwrite(os.path.join(sys.argv[2], os.path.splitext(file)[0]+".png" ), func(os.path.join(sys.argv[1], file)))else:cv2.imwrite(os.path.join(sys.argv[2], os.path.splitext(os.path.basename(sys.argv[1]))[0]+".png"), func(sys.argv[1]))

使用方法:在命令行中输入
python json_to_mask.py 文件夹或json文件 输出文件夹
python mask_to_json.py 文件夹或mask图片 输出文件夹
如果输入文件夹,则会将文件夹下所有json文件或mask图片转成对应文件输出到指定路径中。

来源:Mask图像与json文件相互转换
感谢大佬!

报错问题

在json-to-mask的脚本中可能会出现下面的报错

error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in
function ‘cv::fillPoly’

在这里插入图片描述
可能是由于 OpenCV 的 fillPoly 函数引发的。这个错误通常表示填充多边形时,传递的点的数据类型或结构有问题。
为了解决这个问题,可以在填充之前检查每个形状的点是否为空,如果是空的话,就跳过这个形状。
在这里,添加了一个条件 if len(points) >= 2 来确保 points 中至少包含两个点,如果不满足这个条件,就跳过这个形状的处理。

def func(file:str) -> np.ndarray: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:points = np.array(shape["points"], dtype=np.int32)if len(points) >= 2:cv2.fillPoly(png, [points], (255, 255, 255))#cv2.fillPoly(png, [np.array(shape["points"], np.int32)], (255,255,255))#cv2.fillPoly(png, [np.array(shape["points"], dtype=np.int32)], (255,255,255))

json-to-mask.py

import cv2
import json
import numpy as np
import os
import sysdef func(file:str) -> np.ndarray: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:points = np.array(shape["points"], dtype=np.int32)if len(points) >= 2:cv2.fillPoly(png, [points], (255, 255, 255))#cv2.fillPoly(png, [np.array(shape["points"], np.int32)], (255,255,255))#cv2.fillPoly(png, [np.array(shape["points"], dtype=np.int32)], (255,255,255))return pngif  __name__ == "__main__":if len(sys.argv) != 3:raise ValueError("json文件或目录 输出路径")if os.path.isdir(sys.argv[1]):for file in os.listdir(sys.argv[1]):cv2.imwrite(os.path.join(sys.argv[2], os.path.splitext(file)[0]+".png" ), func(os.path.join(sys.argv[1], file)))else:cv2.imwrite(os.path.join(sys.argv[2], os.path.splitext(os.path.basename(sys.argv[1]))[0]+".png"), func(sys.argv[1]))
http://www.lryc.cn/news/263161.html

相关文章:

  • 华清远见嵌入式学习——ARM——作业1
  • 如何在公网环境使用固定域名远程访问内网BUG管理系统协同办公
  • k8s pod网络排查教程
  • Apollo Planning——换道:LANE_CHANGE_DECIDER
  • Python 爬虫之简单的爬虫(三)
  • 为突发事件提供高现势性数据支撑!大势智慧助力中山市2023应急测绘保障演练举行
  • 图片速览 OOD用于零样本 OOD 检测的 CLIPN:教 CLIP 说不
  • a16z:加密行业2024趋势“无缝用户体验”
  • C# WPF上位机开发(属性页面的设计)
  • macOS 安装 oh-my-zsh 后 node 报错 command not found : node
  • AI 绘画 | Stable Diffusion 视频数字人
  • 《代码随想录》--二叉树(一)
  • shell编程-数组与运算符详解(超详细)
  • Vim入门
  • 动态加载库
  • React中渲染html结构---dangerouslySetInnerHTML
  • 计网02-计算机网络参考模型
  • 模块测试:确保软件质量的关键步骤
  • Postman接口测试之Postman常用的快捷键
  • keil自动分配SDRAM空间设置使用
  • TikTok获客怎么做?可以定制一个获客工具!
  • 数据结构(Chapter Two -02)—顺序表基本操作实现
  • SQL语句整理二--Mysql
  • oracle与gbase8s迁移数据类型对照
  • Flink系列之:集合操作
  • STL:string的常见用法
  • GBASE南大通用 ADO.NET 中的事务
  • App(Android)ICP备案号查询——————高仿微信
  • 修改npm源码解决服务端渲染环境中localstorage报错read properties of undefined (reading getItem)
  • Educational Codeforces Round 160 (Div. 2) A~C(D,E更新中...)