栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

掩码图片转json 坐标数组, mask 转 json

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

掩码图片转json 坐标数组, mask 转 json

主要方法为:findContours 不懂的自行百度吧, 自行修改代码生成json文件
# encoding: utf-8
"""
@author: _Jack Sparrow
@time:  2022/7/28 16:35
@file: mask_as_json.py
@desc: 掩码图片转json
"""

import cv2
import pandas as pd
import os


def get_coor(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 变为灰度图
    #ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)  ## 阈值分割得到二值化图片
    # cv2.namedWindow('binary', cv2.WINDOW_AUTOSIZE)
    # cv2.imshow('binary', binary)
    # cv2.waitKey(0)
    contours, heriachy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    pointsList = []
    print(len(contours)) # 目标个数
    for i, contour in enumerate(contours):
        # print(heriachy)
        if len(contour) < 20:
            continue
        num = len(contour[:, 0, 0])  # 个数
        hundred = num // 80  # 每个点之间的步长
        tem = contour[:, 0][::hundred]
        return tem, 1

if __name__ == '__main__':
    

    img_path = r"c://......"
    img = cv2.imread(img_path)
    h, w = img.shape[:-1]
    contours, flag = get_coor(img)
    # 坐标数组contours

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029778.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号