任意兩個旋轉矩形的IOU計算方法

方法一:

import cv2
import numpy as np
 
image = cv2.imread('。。/Downloads/timg.jpeg')
original_grasp_bboxes  = np.array([[[361, 260.582 ],  [301 ,315], [320 ,336],[380, 281.582]]], dtype = np.int32)
prediction_grasp_bboxes  = np.array([[[301, 290.582 ],  [321 ,322], [310 ,346],[380, 291.582]]], dtype = np.int32)
im = np.zeros(image.shape[:2], dtype = "uint8")
im1 =np.zeros(image.shape[:2], dtype = "uint8")
original_grasp_mask = cv2.fillPoly(im, original_grasp_bboxes, 255)
prediction_grasp_mask = cv2.fillPoly(im1,prediction_grasp_bboxes,255)
masked_and = cv2.bitwise_and(original_grasp_mask,prediction_grasp_mask , mask=im)
masked_or = cv2.bitwise_or(original_grasp_mask,prediction_grasp_mask)
 
or_area = np.sum(np.float32(np.greater(masked_or,0)))
and_area =np.sum(np.float32(np.greater(masked_and,0)))
IOU = and_area/or_area
 
print(or_area)
print(and_area)
print(IOU)

方法二:

# 中心點 矩形的w h, 旋轉的theta(角度,不是弧度)
def iou_rotate_calculate(boxes1, boxes2):
    area1 = boxes1[:, 2] * boxes1[:, 3]
    area2 = boxes2[:, 2] * boxes2[:, 3]
    ious = []
    for i, box1 in enumerate(boxes1):
        temp_ious = []
        r1 = ((box1[0], box1[1]), (box1[2], box1[3]), box1[4])
        for j, box2 in enumerate(boxes2):
            r2 = ((box2[0], box2[1]), (box2[2], box2[3]), box2[4])

            int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1]
            if int_pts is not None:
                order_pts = cv2.convexHull(int_pts, returnPoints=True)

                int_area = cv2.contourArea(order_pts)

                inter = int_area * 1.0 / (area1[i] + area2[j] - int_area)
                temp_ious.append(inter)
            else:
                temp_ious.append(0.0)
        ious.append(temp_ious)
    return np.array(ious, dtype=np.float32)

方法三:

import numpy as np 
import shapely
from shapely.geometry import Polygon,MultiPoint  #多邊形
 
line1=[2,0,2,2,0,0,0,2]   #四邊形四個點座標的一維數組表示,[x,y,x,y....]
a=np.array(line1).reshape(4, 2)   #四邊形二維座標表示
poly1 = Polygon(a).convex_hull  #python四邊形對象,會自動計算四個點,最後四個點順序爲:左上 左下  右下 右上 左上
print(Polygon(a).convex_hull)  #可以打印看看是不是這樣子
 
line2=[1,1,4,1,4,4,1,4]
b=np.array(line2).reshape(4, 2)
poly2 = Polygon(b).convex_hull
print(Polygon(b).convex_hull)
 
union_poly = np.concatenate((a,b))   #合併兩個box座標,變爲8*2
#print(union_poly)
print(MultiPoint(union_poly).convex_hull)      #包含兩四邊形最小的多邊形點
if not poly1.intersects(poly2): #如果兩四邊形不相交
    iou = 0
else:
    try:
        inter_area = poly1.intersection(poly2).area   #相交面積
        print(inter_area)
        #union_area = poly1.area + poly2.area - inter_area
        union_area = MultiPoint(union_poly).convex_hull.area
        print(union_area)
        if union_area == 0:
            iou= 0
        #iou = float(inter_area) / (union_area-inter_area)  #錯了
        iou=float(inter_area) / union_area
        # iou=float(inter_area) /(poly1.area+poly2.area-inter_area)
        # 源碼中給出了兩種IOU計算方式,第一種計算的是: 交集部分/包含兩個四邊形最小多邊形的面積  
        # 第二種: 交集 / 並集(常見矩形框IOU計算方式) 
    except shapely.geos.TopologicalError:
        print('shapely.geos.TopologicalError occured, iou set to 0')
        iou = 0
 
print(a)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章