本文主要是介绍obb iou计算,旋转框iou,python和c++版本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
python版本
import math#包围盒转化为角点
def rbbox_to_corners(rbbox):# generate clockwise corners and rotate it clockwise# 顺时针方向返回角点位置cx, cy, x_d, y_d, angle = rbboxa_cos = math.cos(angle)a_sin = math.sin(angle)corners_x = [-x_d / 2, -x_d / 2, x_d / 2, x_d / 2]corners_y = [-y_d / 2, y_d / 2, y_d / 2, -y_d / 2]corners = [0] * 8for i in range(4):corners[2 *i] = a_cos * corners_x[i] + \a_sin * corners_y[i] + cxcorners[2 * i +1] = -a_sin * corners_x[i] + \a_cos * corners_y[i] + cyreturn corners# 点在四边形(矩形)内?
def point_in_quadrilateral(pt_x, pt_y, corners):ab0 = corners[2] - corners[0]ab1 = corners[3] - corners[1]ad0 = corners[6] - corners[0]ad1 = corners[7] - corners[1]ap0 = pt_x - corners[0]ap1 = pt_y - corners[1]abab = ab0 * ab0 + ab1 * ab1abap = ab0 * ap0 + ab1 * ap1adad = ad0 * ad0 + ad1 * ad1adap = ad0 * ap0 + ad1 * ap1return abab >= abap and abap >= 0 and adad >= adap and adap >= 0# 相交后转化为直线交点
def line_segment_intersection(pts1, pts2, i, j):# pts1, pts2 为corners# i j 分别表示第几个交点,取其和其后一个点构成的线段# 返回为 tuple(bool, pts) bool=True pts为交点A, B, C, D, ret = [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]A[0] = pts1[2 * i]A[1] = pts1[2 * i + 1]B[0] = pts1[2 * ((i + 1) % 4)]B[1] = pts1[2 * ((i + 1) % 4) + 1]C[0] = pts2[2 * j]C[1] = pts2[2 * j + 1]D[0] = pts2[2 * ((j + 1) % 4)]D[1] = pts2[2 * ((j + 1) % 4) + 1]BA0 = B[0] - A[0]BA1 = B[1] - A[1]DA0 = D[0] - A[0]CA0 = C[0] - A[0]DA1 = D[1] - A[1]CA1 = C[1] - A[1]# 叉乘判断方向acd = DA1 * CA0 > CA1 * DA0bcd = (D[1] - B[1]) * (C[0] - B[0]) > (C[1] - B[1]) * (D[0] - B[0])if acd != bcd:abc = CA1 * BA0 > BA1 * CA0abd = DA1 * BA0 > BA1 * DA0# 判断方向if abc != abd:DC0 = D[0] - C[0]DC1 = D[1] - C[1]ABBA = A[0] * B[1
这篇关于obb iou计算,旋转框iou,python和c++版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!