Target detection IOU calculation

IOU is the Intersection-over-Union (Intersection-over-Union) is a concept used in target detection is the overlap rate of the generated candidate bound and the ground truth bound, that is, their intersection and union. Ratio . The ideal situation is complete overlap, that is, the ratio is 1. In multi-target tracking, it is used to determine the similarity between the tracking frame and the target detection frame.

IoU is the result of the intersection of two regions divided by the sum of the two regions

from __future__ import print_function
from numba import jit
import numpy as np


# 计算IOU
@jit
def iou(bb_test, bb_gt):
    """
    计算预测框和目标框的交并比
    :param bb_test:表示预测框的左上角和右下角的坐标 [x1, y1, x2, y2]
    :param bb_gt: 表示目标框的左上角和右下角的坐标 [x1, y1, x2, y2]
    :return:
    """
    # 交集区域左上角坐标的最大值
    xx1 = np.maximum(bb_test[0], bb_gt[0])
    yy1 = np.maximum(bb_test[1], bb_gt[1])

    # 交集区域右下角坐标的最大值
    xx2 = np.maximum(bb_test[2], bb_gt[2])
    yy2 = np.maximum(bb_test[3], bb_gt[3])

    # 交集区域的宽
    w = np.maximum(0, xx2 - xx1)
    # 交集区域的高
    h = np.maximum(0, yy2 - yy1)
    # 交集区域的面积
    hw = h * w

    # 并集的面积
    s = (bb_test[2] - bb_test[0]) * (bb_test[3] - bb_test[1]) + (bb_gt[2] - bb_gt[0]) * (bb_gt[3] - bb_gt[1]) - hw

    return hw/s

if __name__ == '__main__':
    print(iou([133, 59, 310, 213], [182, 94, 327, 248]))
    print(iou([182, 94, 327, 248], [133, 59, 310, 213]))

 

Guess you like

Origin blog.csdn.net/qq_39197555/article/details/114952970