当前位置:   article > 正文

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

opencv obb

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

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

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

  1. import numpy as np
  2. import cv2
  3. # 创建一个全白图像
  4. image = np.ones((1080, 1920, 3), dtype=np.uint8) * 255
  5. for i, poly in enumerate(pre):
  6. polygon_list = np.array(poly, np.int32)
  7. cv2.drawContours(image, contours=[polygon_list], contourIdx=-1, color=(0, 0, 255), thickness=2)
  8. for i, poly in enumerate(post):
  9. polygon_list = np.array(poly, np.int32)
  10. cv2.drawContours(image, contours=[polygon_list], contourIdx=-1, color=(255, 0, 0), thickness=2)
  11. cv2.imshow("Image", image)
  12. cv2.waitKey(0)
  13. cv2.destroyAllWindows()

opencv将这些坐标画出来:

方法一

使用opencv内置函数计算iou

  1. import math
  2. def poly2rbox(polys):
  3. """
  4. Trans poly format to rbox format.
  5. """
  6. assert polys.shape[-1] == 8
  7. rboxes = []
  8. for poly in polys:
  9. poly = np.float32(poly.reshape(4, 2))
  10. (x, y), (w, h), angle = cv2.minAreaRect(poly) # θ ∈ [0, 90]
  11. rboxes.append([x, y, w, h, angle])
  12. return np.array(rboxes)
  13. def bbox_overlaps(boxes, query_boxes):
  14. """ Calculate IoU(intersection-over-union) and angle difference for each input boxes and query_boxes. """
  15. if isinstance(boxes, list):
  16. boxes = np.array(boxes)
  17. if isinstance(query_boxes, list):
  18. query_boxes = np.array(query_boxes)
  19. N = boxes.shape[0]
  20. K = query_boxes.shape[0]
  21. boxes = np.round(boxes, decimals=2)
  22. query_boxes = np.round(query_boxes, decimals=2)
  23. overlaps = np.reshape(np.zeros((N, K)), (N, K))
  24. delta_theta = np.reshape(np.zeros((N, K)), (N, K))
  25. for k in range(K):
  26. rect1 = ((query_boxes[k][0], query_boxes[k][1]),
  27. (query_boxes[k][2], query_boxes[k][3]),
  28. query_boxes[k][4])
  29. for n in range(N):
  30. rect2 = ((boxes[n][0], boxes[n][1]),
  31. (boxes[n][2], boxes[n][3]),
  32. boxes[n][4])
  33. # can check official document of opencv for details
  34. num_int, points = cv2.rotatedRectangleIntersection(rect1, rect2)
  35. S1 = query_boxes[k][2] * query_boxes[k][3]
  36. S2 = boxes[n][2] * boxes[n][3]
  37. if num_int == 1 and len(points) > 2:
  38. s = cv2.contourArea(cv2.convexHull(points, returnPoints=True))
  39. overlaps[n][k] = s / (S1 + S2 - s)
  40. elif num_int == 2:
  41. overlaps[n][k] = min(S1, S2) / max(S1, S2)
  42. delta_theta[n][k] = np.abs(query_boxes[k][4] - boxes[n][4])
  43. return overlaps, delta_theta
  44. pre = poly2rbox(np.array(pre).reshape(-1,8))
  45. post = poly2rbox(np.array(post).reshape(-1,8))
  46. overlaps = bbox_overlaps(pre, post)[0]
  47. print(overlaps)

运行结果如下: 

方法二

使用shapely

  1. from shapely.geometry import Polygon
  2. def calculate_iou(poly1, poly2):
  3. # 计算两个多边形的交集面积
  4. intersection_area = calculate_intersection(poly1, poly2)
  5. # 计算两个多边形的并集面积
  6. union_area = calculate_union(poly1, poly2)
  7. # 计算IoU值
  8. iou = intersection_area / union_area
  9. return iou
  10. def calculate_intersection(poly1, poly2):
  11. # 计算多边形的交集面积
  12. # 这里使用你选择的多边形交集计算方法,例如使用Shapely库的intersection()函数
  13. intersection = poly1.intersection(poly2)
  14. intersection_area = intersection.area
  15. return intersection_area
  16. def calculate_union(poly1, poly2):
  17. # 计算多边形的并集面积
  18. # 这里使用你选择的多边形并集计算方法,例如使用Shapely库的union()函数
  19. union = poly1.union(poly2)
  20. union_area = union.area
  21. return union_area
  22. def bbox_overlaps_shapely(boxes, query_boxes):
  23. """ Calculate IoU(intersection-over-union) and angle difference for each input boxes and query_boxes. """
  24. if isinstance(boxes, list):
  25. boxes = np.array(boxes)
  26. if isinstance(query_boxes, list):
  27. query_boxes = np.array(query_boxes)
  28. N = boxes.shape[0]
  29. K = query_boxes.shape[0]
  30. boxes = np.round(boxes, decimals=2)
  31. query_boxes = np.round(query_boxes, decimals=2)
  32. overlaps = np.reshape(np.zeros((N, K)), (N, K))
  33. delta_theta = np.reshape(np.zeros((N, K)), (N, K))
  34. for k in range(K):
  35. q_box = Polygon(query_boxes[k].reshape(-1, 2).tolist())
  36. for n in range(N):
  37. d_box = Polygon(boxes[n].reshape(-1, 2).tolist())
  38. overlaps[n][k] = calculate_iou(q_box, d_box)
  39. return overlaps, delta_theta
  40. overlaps = bbox_overlaps_shapely(np.array(pre).reshape(-1,8),np.array(post).reshape(-1,8))[0]
  41. print(overlaps)

运行结果如下:

方法三

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/114185?site
推荐阅读
相关标签
  

闽ICP备14008679号