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

2023-11-30 20:36

本文主要是介绍旋转框(obb)目标检测计算iou的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

 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 npimport cv2# 创建一个全白图像image = np.ones((1080, 1920, 3), dtype=np.uint8) * 255for 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 detailsnum_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_thetaoverlaps = bbox_overlaps(np.array(pre).reshape(-1,8),np.array(post).reshape(-1,8))[0]print(overlaps)

运行结果如下: 

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

方法二

使用shapely

    from shapely.geometry import Polygondef calculate_iou(poly1, poly2):# 计算两个多边形的交集面积intersection_area = calculate_intersection(poly1, poly2)# 计算两个多边形的并集面积union_area = calculate_union(poly1, poly2)# 计算IoU值iou = intersection_area / union_areareturn ioudef calculate_intersection(poly1, poly2):# 计算多边形的交集面积# 这里使用你选择的多边形交集计算方法,例如使用Shapely库的intersection()函数intersection = poly1.intersection(poly2)intersection_area = intersection.areareturn intersection_areadef calculate_union(poly1, poly2):# 计算多边形的并集面积# 这里使用你选择的多边形并集计算方法,例如使用Shapely库的union()函数union = poly1.union(poly2)union_area = union.areareturn union_areadef 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_thetaoverlaps = bbox_overlaps_shapely(np.array(pre).reshape(-1,8),np.array(post).reshape(-1,8))[0]print(overlaps)

运行结果如下:

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

方法三

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

这篇关于旋转框(obb)目标检测计算iou的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/438453

相关文章

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin