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

labelme COCO格式数据 与 左上角右下角格式数据 相互转化

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

labelme COCO格式数据 与 左上角右下角格式数据 相互转化

import cv2


class DataConvert():
    """
    表达方式	说明
    x1,y1,x2,y2	(x1,y1)为左上角坐标,(x2,y2)为右下角坐标
    x1,y1,w,h	(x1,y1)为左上角坐标,w为目标区域宽度,h为目标区域高度
    xc,yc,w,h	(xc,yc)为目标区域中心坐标,w为目标区域宽度,h为目标区域高度 COCO标注
    """

    def __init__(self):
        pass

    @staticmethod
    def cvtx0y0whTox1y1x2y2(x0, y0, w, h, imgShape):
        # "0.530921 0.666667 0.622368 0.666667"=>(167, 169, 639, 507)
        # labelme 的COCO标注格式就是 中心点x+中心点y+宽+高 (归一化的)
        # 此函数出来的就是 左上点  右下点  (未归一化的)
        height, width, c = imgShape
        x1, y1, x2, y2 = int((x0 - w * 0.5) * width), 
                         int((y0 - h * 0.5) * height), 
                         int((x0 + w * 0.5) * width), 
                         int((y0 + h * 0.5) * height)
        return x1, y1, x2, y2

    @staticmethod
    def cvtx1y1x2y2Tox0y0wh(x1, y1, x2, y2, imgShape):
        # (167, 169, 639, 507)=>"0.530921 0.666667 0.622368 0.666667"
        # 左上点  右下点  (未归一化的) => 中心点x+中心点y+宽+高 (归一化的)
        height, width, c = imgShape
        x0, y0, w, h = (x1 + x2) / 2 / width, (y1 + y2) / 2 / height, (x2 - x1) / width, (y2 - y1) / height,
        return x0, y0, w, h


if __name__ == '__main__':
    img = cv2.imread("jg02387.jpg")
    shape = img.shape
    x1, y1, x2, y2 = DataConvert.cvtx0y0whTox1y1x2y2(0.530921, 0.666667, 0.622368, 0.666667, shape)
    print(x1, y1, x2, y2)
    print(DataConvert.cvtx1y1x2y2Tox0y0wh(x1, y1, x2, y2, shape))

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

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

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