旋转框(obb)目标检测计算iou的方法

news/2024/7/10 0:01:07 标签: 目标检测, opencv, 计算机视觉

首先先定义一组多边形,这里的数据来自前后帧的检测结果

 pre = [[[860.0, 374.0], [823.38, 435.23], [716.38, 371.23], [753.0, 310.0]],
         [[829.0, 465.0], [826.22, 544.01], [684.0, 539.0], [686.78, 459.99]],
         [[885.72, 574.95], [891.0, 648.0], [725.0, 660.0], [719.72, 586.95]],
         [[1164.0, 406.0], [1101.05, 410.72], [1095.0, 330.0], [1157.95, 325.28]],
         [[953.04, 102.78], [955.04, 138.78], [915.0, 141.0], [913.0, 105.0]],
         [[1173.0, 524.0], [1104.0, 524.0], [1104.0, 437.0], [1173.0, 437.0]],
         [[879.0, 297.0], [831.45, 340.49], [756.0, 258.0], [803.55, 214.51]],
         [[1136.79, 226.81], [1176.33, 263.31], [1111.54, 333.5], [1072.0, 297.0]],
         [[835.42, 225.76], [790.0, 251.0], [750.66, 180.19], [796.08, 154.95]],
         [[887.0, 196.0], [839.04, 208.16], [821.0, 137.0], [868.96, 124.84]],
         [[1033.0, 109.0], [1027.07, 142.01], [988.0, 135.0], [993.93, 101.99]],
         [[1056.0, 83.0], [1093.09, 90.53], [1080.0, 155.0], [1042.91, 147.47]],
         [[1064.01, 155.84], [1104.0, 158.0], [1099.99, 232.16], [1060.0, 230.0]],
         [[1087.06, 118.88], [1124.0, 137.0], [1097.94, 190.12], [1061.0, 172.0]]]
    post = [[[860.44, 373.25], [825.0, 434.0], [716.56, 370.75], [752.0, 310.0]],
         [[829.0, 466.0], [825.64, 545.03], [684.64, 539.03], [688.0, 460.0]],
         [[884.04, 575.0], [889.0, 649.0], [724.96, 660.0], [720.0, 586.0]],
         [[1163.0, 406.0], [1100.0, 410.0], [1094.92, 329.94], [1157.92, 325.94]],
         [[953.0, 103.0], [955.56, 137.96], [914.56, 140.96], [912.0, 106.0]],
         [[1173.0, 524.0], [1104.0, 524.0], [1104.0, 438.0], [1173.0, 438.0]],
         [[880.0, 297.0], [831.0, 342.0], [755.34, 259.61], [804.34, 214.61]],
         [[1137.31, 226.66], [1177.0, 263.0], [1112.0, 334.0], [1072.31, 297.66]],
         [[887.06, 194.23], [840.0, 207.0], [820.94, 136.77], [868.0, 124.0]],
         [[836.69, 224.57], [792.69, 251.57], [750.0, 182.0], [794.0, 155.0]],
         [[1033.0, 106.0], [1030.0, 143.0], [987.95, 139.59], [990.95, 102.59]],
         [[1055.95, 83.27], [1094.0, 91.0], [1081.0, 155.0], [1042.95, 147.27]],
         [[1064.0, 155.0], [1105.02, 156.05], [1103.02, 234.05], [1062.0, 233.0]],
         [[1081.72, 120.74], [1120.0, 135.0], [1101.0, 186.0], [1062.72, 171.74]]]

其中的每个列表元素代表一个多边形,列表中包含四个元素,分别代表多边形的顶点坐标

    import numpy as np
    import cv2
    # 创建一个全白图像
    image = np.ones((1080, 1920, 3), dtype=np.uint8) * 255

    for i, poly in enumerate(pre):
        polygon_list = np.array(poly, np.int32)
        cv2.drawContours(image, contours=[polygon_list], contourIdx=-1, color=(0, 0, 255), thickness=2)
    for i, poly in enumerate(post):
        polygon_list = np.array(poly, np.int32)
        cv2.drawContours(image, contours=[polygon_list], contourIdx=-1, color=(255, 0, 0), thickness=2)

    cv2.imshow("Image", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

opencv将这些坐标画出来:

方法一

使用opencv内置函数计算iou

    def bbox_overlaps(boxes, query_boxes):
        """ Calculate IoU(intersection-over-union) and angle difference for each input boxes and query_boxes. """
        if isinstance(boxes, list):
            boxes = np.array(boxes)
        if isinstance(query_boxes, list):
            query_boxes = np.array(query_boxes)
        N = boxes.shape[0]
        K = query_boxes.shape[0]
        boxes = np.round(boxes, decimals=2)
        query_boxes = np.round(query_boxes, decimals=2)
        overlaps = np.reshape(np.zeros((N, K)), (N, K))
        delta_theta = np.reshape(np.zeros((N, K)), (N, K))

        for k in range(K):
            rect1 = ((query_boxes[k][0], query_boxes[k][1]),
                     (query_boxes[k][2], query_boxes[k][3]),
                     query_boxes[k][4])
            for n in range(N):
                rect2 = ((boxes[n][0], boxes[n][1]),
                         (boxes[n][2], boxes[n][3]),
                         boxes[n][4])
                # can check official document of opencv for details
                num_int, points = cv2.rotatedRectangleIntersection(rect1, rect2)
                S1 = query_boxes[k][2] * query_boxes[k][3]
                S2 = boxes[n][2] * boxes[n][3]
                if num_int == 1 and len(points) > 2:
                    s = cv2.contourArea(cv2.convexHull(points, returnPoints=True))
                    overlaps[n][k] = s / (S1 + S2 - s)
                elif num_int == 2:
                    overlaps[n][k] = min(S1, S2) / max(S1, S2)
                delta_theta[n][k] = np.abs(query_boxes[k][4] - boxes[n][4])
        return overlaps, delta_theta

    overlaps = bbox_overlaps(np.array(pre).reshape(-1,8),np.array(post).reshape(-1,8))[0]
    print(overlaps)

运行结果如下: 

可以看到其中存在一些异常值,就是有些明明没有交集的部分也会产生比较高的iou值

方法二

使用shapely

    from shapely.geometry import Polygon
    def calculate_iou(poly1, poly2):
        # 计算两个多边形的交集面积
        intersection_area = calculate_intersection(poly1, poly2)
        # 计算两个多边形的并集面积
        union_area = calculate_union(poly1, poly2)
        # 计算IoU值
        iou = intersection_area / union_area
        return iou

    def calculate_intersection(poly1, poly2):
        # 计算多边形的交集面积
        # 这里使用你选择的多边形交集计算方法,例如使用Shapely库的intersection()函数
        intersection = poly1.intersection(poly2)
        intersection_area = intersection.area
        return intersection_area

    def calculate_union(poly1, poly2):
        # 计算多边形的并集面积
        # 这里使用你选择的多边形并集计算方法,例如使用Shapely库的union()函数
        union = poly1.union(poly2)
        union_area = union.area
        return union_area

    def bbox_overlaps_shapely(boxes, query_boxes):
        """ Calculate IoU(intersection-over-union) and angle difference for each input boxes and query_boxes. """
        if isinstance(boxes, list):
            boxes = np.array(boxes)
        if isinstance(query_boxes, list):
            query_boxes = np.array(query_boxes)
        N = boxes.shape[0]
        K = query_boxes.shape[0]
        boxes = np.round(boxes, decimals=2)
        query_boxes = np.round(query_boxes, decimals=2)
        overlaps = np.reshape(np.zeros((N, K)), (N, K))
        delta_theta = np.reshape(np.zeros((N, K)), (N, K))

        for k in range(K):
            q_box = Polygon(query_boxes[k].reshape(-1, 2).tolist())
            for n in range(N):
                d_box = Polygon(boxes[n].reshape(-1, 2).tolist())
                overlaps[n][k] = calculate_iou(q_box, d_box)
        return overlaps, delta_theta

    overlaps = bbox_overlaps_shapely(np.array(pre).reshape(-1,8),np.array(post).reshape(-1,8))[0]
    print(overlaps)

运行结果如下:

可以看到这个结果相比方法一中的结果要更加准确一些 

方法三

cuda内置的函数,需要编译环境,就不展开了


http://www.niftyadmin.cn/n/5228461.html

相关文章

C#多种类的调用(模拟银行管理系统)(存在bug)

前言: 大家一起学习进步,提出改进意见 代码实现: using System; using System.Reflection;namespace FIfthtest_banksystem {public class Program{static void Main(string[] args){Account[] accounts new Account[3];{new Account { C…

基于SpringBoot的旅游网站的设计与实现

摘 要 随着科学技术的飞速发展,各行各业都在努力与现代先进技术接轨,通过科技手段提高自身的优势,旅游网站当然也不能排除在外,随着旅游网站的不断成熟,它彻底改变了过去传统的旅游网站方式,不仅使旅游管理…

数字图像处理(实践篇)十二 基于小波变换的图像降噪

目录 一 基于小波变换的图像降噪 (1)小波变换基本理论 (2)小波分析在图像处理中的应用 (3)小波变换原理 (4)小波降噪原理 (5)小波降噪算法的实现 &…

WTM(基于Blazor)问题处理记录

问题描述一 有个需求,需要访问内网网络共享文件夹中的文件,有域控限制。 一开始直接在本地映射一个网络驱动器,然后像本地磁盘一样访问共享文件夹里的文件,比如:Y:\ 。 然后直接在程序中访问共享文件夹中的文件,如下代码: DirectoryInfo directoryInfo = new Direct…

数据结构:图文详解顺序表的各种操作(新增元素,查找元素,删除元素,给指定位置元素赋值)

目录 一.顺序表的概念 二.顺序表的实现 新增元素 默认尾部新增 指定位置添加元素 查找元素 查找是否存在 查找元素对应的位置 查找指定位置对应的元素 删除元素 获取顺序表长度 清空顺序表 一.顺序表的概念 在线性数据结构中,我们一般分为俩类&#xf…

【人工智能Ⅰ】实验6:回归预测实验

实验6 回归预测实验 一、实验目的 1:了解机器学习中数据集的常用划分方法以及划分比例,并学习数据集划分后训练集、验证集及测试集的作用。 2:了解降维方法和回归模型的应用。 二、实验要求 数据集(LUCAS.SOIL_corr-实验6数据…

ERROR:sf is not compatible with GDAL version below 2.0.1

在安装monocle3时,出现报错信息如下: devtools::install_github(cole-trapnell-lab/monocle3) 显示GDAL版本不对,必须得更新到2.0.1以上,于是尝试更新版本。 sudo add-apt-repository -y ppa:ubuntugis/ppa sudo apt update su…

rabbitmq消息队列实验

实验目的:实现异步通信 实验条件: 主机名 IP地址 组件 test1 20.0.0.10 rabbitmq服务 test2 20.0.0.20 rabbitmq服务 test3 20.0.0.30 rabbitmq服务 实验步骤: 1、安装rabbitmq服务 2、erlang进入命令行,查看版本 …