掩码图片转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

你可能感兴趣的:(json,python,opencv,深度学习)