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

X-AnyLabeling系列:多类别单通道掩码转多边形坐标点

多类别单通道掩码转多边形坐标点

整体与掩码转多边形坐标点类似,主要是单类别单通道掩码改为多类别单通道掩码。

示例代码

import os
import cv2
import json
import numpy as np
from copy import deepcopydef 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(mask, shape_dict, image_name,epsilon_factor,approx_len_threshold,point_interval_num=1
):contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)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])return shape_dictdef multiclass_mask_to_polygon(image_path, mask_path, json_path,mask_label_list, mask_color_list,shape_dict, epsilon_factor, approx_len_threshold,point_interval_num=1,
):# mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)mask = cv2.imdecode(np.fromfile(mask_path, dtype=np.uint8), 0)image_name = os.path.basename(image_path)image_width, image_height = get_image_size(image_path)custom_data = reset("2.4.0")custom_data["imagePath"] = image_namecustom_data["imageHeight"] = image_heightcustom_data["imageWidth"] = image_widthfor label, color in zip(mask_label_list, mask_color_list):# 选择特定颜色的maskmask_label = cv2.inRange(mask, color, color)mask_label_shape_dict = deepcopy(shape_dict)mask_label_shape_dict["label"] = labelmask_label_shape_dict = mask_to_polygon(mask_label, mask_label_shape_dict, image_name,epsilon_factor,approx_len_threshold,point_interval_num,)custom_data["shapes"].append(mask_label_shape_dict)with open(json_path, "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 = "./json/test.json"mask_label_list = ["label_1", "label_2"]mask_color_list = [255, 128]shape_dict = {"label": "","description": "","difficult": False,"points": [],"shape_type": "polygon",}multiclass_mask_to_polygon(image_path, mask_path, json_path,mask_label_list, mask_color_list,shape_dict, epsilon_factor=0.001, approx_len_threshold=5,point_interval_num=2,)

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

相关文章:

  • XML 实例:深入解析与实际应用
  • 网上花店设计+vue
  • 数据库如何插入优化分别用oracle和mysql举例
  • C ++初阶:C++入门级知识点
  • CSS的:nth-last-child()伪类:逆序子元素选择的艺术
  • commvault学习(9):备份mysql(linux平台)
  • Ubuntu启用root用户
  • 【django进阶知识点】
  • Frida实战:枚举模块、导入表、导出表与符号表
  • 结构型模式之组合模式
  • YOLOv10:实时端到端目标检测
  • IDM下载器怎么提速 IDM下载速度慢解决办法 idm下载器设置怎么下载最快
  • git打包某个分支的特定提交
  • 【SpringBoot】开发常见注解
  • RabbitMQ中如何通过死信交换机实现延时队列
  • MFC使用双缓冲实现绘制字符串防闪烁
  • 书生大模型实战营第三期基础岛第二课——8G 显存玩转书生大模型 Demo
  • 8.21面试复盘
  • 彻底解决win7系统文件夹选项高级设置是空白
  • 在Ubuntu 部署 Grafana且监控MySQL数据