读取JSON文件,根据mask判断是否连通

该博客介绍了如何读取COCO格式的JSON文件,并利用maskUtils库进行图像分割,通过findContours函数查找并可视化轮廓。代码实现了从mask_ann到图像掩模的转换,并展示了一个具体的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义coco JSON文件 (instances_val2017.json)

1. 读取JSON文件

json_path = 'instances_val2017.json'
main = open(json_path)
main = json.load(main)

2. 根据mask查找

solov2源码(mmdet/datasets/pipelines/loading.py)进行了简单修改

def _poly2mask(mask_ann, img_h, img_w):
    if isinstance(mask_ann, list):
        # polygon -- a single object might consist of multiple parts
        # we merge all parts into one mask rle code
        rles = maskUtils.frPyObjects(mask_ann, img_h, img_w)
        rle = maskUtils.merge(rles)
    elif isinstance(mask_ann['counts'], list):
        # uncompressed RLE
        rle = maskUtils.frPyObjects(mask_ann, img_h, img_w)
    else:
        # rle
        rle = mask_ann
    mask = maskUtils.decode(rle)
    return mask
    
def check_mask(main):
    for i in range(len(main['annotations'])):
        h, w = main['annotations'][i]['height'], main['annotations'][i]['width']
        gt_masks = main['annotations'][i]['segmentation']
        gt_masks = _poly2mask(gt_masks, h, w)  
        image_np = gt_masks
        contours, hierarchy = cv2.findContours(image_np, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        print(len(contours))
        #可视化
        image_show = image_np * 255
        image_show = cv2.cvtColor(image_show, cv2.COLOR_GRAY2BGR)
        image_show = cv2.drawContours(image_show, contours, -1, (0, 0, 255), 3)
        cv2.namedWindow('1', 0)
        cv2.resizeWindow('1', 1024, 1300)
        cv2.imshow("1", image_show)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        print('i:', i)

3.所有需要调用的库

import json
import os
import cv2
import pycocotools.mask as maskUtils

4.整体代码

import json
import os
import cv2
import pycocotools.mask as maskUtils

def _poly2mask(mask_ann, img_h, img_w):
    if isinstance(mask_ann, list):
        # polygon -- a single object might consist of multiple parts
        # we merge all parts into one mask rle code
        rles = maskUtils.frPyObjects(mask_ann, img_h, img_w)
        rle = maskUtils.merge(rles)
    elif isinstance(mask_ann['counts'], list):
        # uncompressed RLE
        rle = maskUtils.frPyObjects(mask_ann, img_h, img_w)
    else:
        # rle
        rle = mask_ann
    mask = maskUtils.decode(rle)
    return mask


def cho_class(main):
    for i in range(len(main['annotations'])):
        h, w = main['annotations'][i]['height'], main['annotations'][i]['width']
        gt_masks = main['annotations'][i]['segmentation']
        gt_masks = _poly2mask(gt_masks, h, w)  # [_poly2mask(mask, h, w) for mask in gt_masks]
        image_np = gt_masks
        contours, hierarchy = cv2.findContours(image_np, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        print(len(contours))
        image_show = image_np * 255
        image_show = cv2.cvtColor(image_show, cv2.COLOR_GRAY2BGR)
        image_show = cv2.drawContours(image_show, contours, -1, (0, 0, 255), 3)

        cv2.namedWindow('demo', 0)
        cv2.resizeWindow('demo', 1024, 1300)
        cv2.imshow("demo", image_show)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        print('i:', i)


if __name__ == '__main__':
    json_path = 'instances_val2017.json'
    main = open(json_path)
    main = json.load(main)
    cho_class(main)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值