博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
可视化显示json文件中的bbox和seg(labelme标注)
阅读量:2050 次
发布时间:2019-04-28

本文共 2620 字,大约阅读时间需要 8 分钟。

import jsonimport globimport osimport cv2import numpy as npred = (0, 0, 255)green = (0, 255, 0)blue = (255, 0, 0)cyan = (255, 255, 0)yellow = (0, 255, 255)magenta = (255, 0, 255)white = (255, 255, 255)black = (0, 0, 0)  def get_pascal_labels():    """Load the mapping that associates pascal classes with label colors    Returns:        np.ndarray with dimensions (21, 3)    """    return np.asarray([[0, 0, 0], [128, 128, 0], [0, 128, 0], [128, 128, 0],                       [0, 0, 128], [128, 0, 128], [0, 128, 128], [128, 128, 128],                       [64, 0, 0], [192, 0, 0], [64, 128, 0], [192, 128, 0],                       [64, 0, 128], [192, 0, 128], [64, 128, 128], [192, 128, 128],                       [0, 64, 0], [128, 64, 0], [0, 192, 0], [128, 192, 0],                       [0, 64, 128]])color_map = get_pascal_labels()WSI_MASK_PATH = './11'paths = glob.glob(os.path.join(WSI_MASK_PATH, '*.png'))paths.sort()for pa in range(len(paths)):    imgs = cv2.imread(paths[pa], -1)    name = paths[pa].split('/')[-1].split('.png')[0]    onejson = paths[pa].replace('.png','.json')    with open(onejson,"r") as load_f:        load_dict = json.load(load_f)        shapes = load_dict['shapes']        seg = []        bbox = []        for num in range(len(shapes)):            if shapes[num]['shape_type']=='rectangle':                bbox.append(shapes[num]['points'])            if shapes[num]['shape_type']=='polygon':                seg.append(shapes[num]['points'])                for kk in range(len(seg)):            mask = np.zeros(imgs.shape[:2], dtype="uint8")            arrayseg = np.array([seg[kk]], dtype = np.int32)            cv2.polylines(mask, arrayseg, 1, 255)            cv2.fillPoly(mask, arrayseg, 255)            nclass = 2            for i in range(nclass):                if i != 0:                    index = np.argwhere(mask != 0)                    for key in index:                        # indexbbox[1]=y indexbbox[0]=x                        # key[0]=h key[1]=w                        imgs[key[0]][key[1]][0] = \                            imgs[key[0]][key[1]][0] * 0.5 + color_map[i][0] * 0.5                        imgs[key[0]][key[1]][1] = \                            imgs[key[0]][key[1]][1] * 0.5 + color_map[i][1] * 0.5                        imgs[key[0]][key[1]][2] = \                            imgs[key[0]][key[1]][2] * 0.5 + color_map[i][2] * 0.5        for j in range(len(bbox)):            img2=cv2.rectangle(imgs,(int(bbox[j][0][0]),int(bbox[j][0][1])),(int(bbox[j][1][0]),int(bbox[j][1][1])),green,3)        cv2.imwrite("./" + name + "_result.png", img2)        a = 1

 

转载地址:http://megof.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《每日一题》20200621 124.二叉树的最大路径和
查看>>
Leetcode C++《每日一题》20200622 面试题 16.18. 模式匹配
查看>>
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
Leetcode C++ 剑指 Offer 09. 用两个栈实现队列
查看>>
Leetcode C++《每日一题》20200707 112. 路径总和
查看>>
云原生 第十一章 应用健康
查看>>
Leetcode C++ 《第202场周赛》
查看>>
云原生 第十二章 可观测性:监控与日志
查看>>
Leetcode C++ 《第203场周赛》
查看>>
云原生 第十三章 Kubernetes网络概念及策略控制
查看>>
《redis设计与实现》 第一部分:数据结构与对象 || 读书笔记
查看>>
《redis设计与实现》 第二部分(第9-11章):单机数据库的实现
查看>>
算法工程师 面经2019年5月
查看>>