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

X-AnyLabeling系列:掩码转多边形坐标点

掩码转多边形坐标点

代码参考: X-AnyLabeling中的.\X-AnyLabeling\ tools\polygon_mask_conversion.py。这里主要拆分出掩码转成多边形坐标点存储在json文件的部分,同时增加了参数和支持带有中文名的路径。

代码示例

import os
import cv2
import json
import numpy as npdef get_image_size(image_file):image = cv2.imdecode(np.fromfile(image_file, dtype=np.uint8), 0)height, width = image.shape[:2]return width, heightdef reset(version):custom_data = dict(version=version,flags={},shapes=[],imagePath="",imageData=None,imageHeight=-1,imageWidth=-1,)return custom_datadef mask_to_polygon(img_file, mask_file, json_file,version,epsilon_factor=0.001, shape_dict=None,approx_len_threshold=5
):custom_data = reset(version)# binary_mask = cv2.imread(mask_file, cv2.IMREAD_GRAYSCALE)binary_mask = cv2.imdecode(np.fromfile(mask_file, dtype=np.uint8), 0)contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)image_name = os.path.basename(img_file)for contour in contours:epsilon = epsilon_factor * cv2.arcLength(contour, True)approx = cv2.approxPolyDP(contour, epsilon, True)if len(approx) < approx_len_threshold:print(f"{image_name}: \contour too small, len={len(approx)}")continuefor point in approx:x, y = point[0].tolist()shape_dict["points"].append([x, y])custom_data["shapes"].append(shape_dict)image_width, image_height = get_image_size(img_file)custom_data["imagePath"] = image_namecustom_data["imageHeight"] = image_heightcustom_data["imageWidth"] = image_widthwith open(json_file, "w", encoding="utf-8") as f:json.dump(custom_data, f, indent=2, ensure_ascii=False)if __name__ == "__main__":image_path = "./images/test.jpg"mask_path = "./masks/test.png"json_path = "./jsons/test.json"shape_dict = {"label": "test","text": "test text","points": [],"group_id": None,"shape_type": "polygon","flags": {},}mask_to_polygon(image_path, mask_path, json_path, epsilon_factor=0.001,version="2.4.0",shape_dict=shape_dict,approx_len_threshold=5)

http://www.mrgr.cn/news/3798.html

相关文章:

  • driver.find_element 找不到元素的大坑
  • Qt第十七章 多线程
  • 生信圆桌x生信菜鸟团:生物信息学初学者的交流与成长社区
  • Qt/QML学习-ScrollView
  • 5. container_of 宏的定义、作用及手动实现详细解释
  • pyqt 用lamada关联信号 传递参数 循环
  • DNF攻略:护石符文体系辅助详解,VMOS云手机助攻核心玩法!
  • macOS symbol(s) not found for architecture arm64错误原因总结
  • 理解 CSS 层叠、优先级和继承——WEB开发系列15
  • springboot jar是如何启动的
  • select模型实现TCP聊天室
  • 连锁店收银系统源码-线下收银多端视频展示
  • GPT-4o微调功能现已上线
  • Redis十大数据类型
  • 【HTML】弹性盒子 (display: flex) 布局
  • web前端之选项卡的实现、动态添加类名、动态移除类名、动态添加样式、激活、tabBar
  • 南大-ICS2021 PA1~PA2.2 学习笔记记录
  • 计算机毕业设计Python深度学习房价预测 房价可视化 链家爬虫 房源爬虫 房源可视化 卷积神经网络 大数据毕业设计 机器学习 人工智能 AI
  • 如何生成随机数(通过rand函数,srand函数,time函数深入讲解)
  • BaseCTF 高校联合新生赛Week1(web)