任意两个旋转矩形的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)
  • 11
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值