首页 > 解决方案 > 如何在所有列组合的两个指标之间进行 numpy 计算?

问题描述

我有以下尝试计算我从人脸检测器获得的几个检测的 IOU。

import numpy as np


class BboxUtils(object):
    @staticmethod
    def ious(bboxes1, bboxes2):
        left1, top1, right1, bottom1 = BboxUtils.bbox_to_perimiters(bboxes1)
        left2, top2, right2, bottom2 = BboxUtils.bbox_to_perimiters(bboxes2)

        area1 = (right1 - left1) * (top1 - bottom1)
        area2 = (right2 - left2) * (top2 - bottom2)

        intersection_left = np.maximum(left1, left2)
        intersection_right = np.minimum(right1, right2)
        intersection_top = np.maximum(top1, top2)
        intersection_bottom = np.minimum(bottom1, bottom2)
        intersection_area = (intersection_right - intersection_left) * (intersection_top - intersection_bottom)
        intersection_area[intersection_area < 0] = 0

        iou = intersection_area / (area1 + area2 - intersection_area)

        return iou

    @staticmethod
    def bbox_to_perimiters(bboxes):
        left, w, top, h = np.split(bboxes.reshape(-1), 4)
        right = left + w
        bottom = top + h
        return left, right, top, bottom


# example usage:
detections1 = my_detector.detect(frame1) #np.array of shape (n1, 4)
detections2 = my_detector.detect(frame2) #np.array of shape (n2, 4)
ious = BboxUtils.ious(detections1, detections2)

此代码假定:

  1. 2 帧 (bboxes1bboxes2) 上的检测长度相同
  2. 每个检测的索引在bboxes1bboxes2

我想用与上面代码相​​同的逻辑来计算 IOU,但要避免 for loops

注意bboxes1bboxes2可以是形状矩阵(n1, 4)(n2, 4),其中n1不一定相等n2

如何才能做到这一点?

最后,可能有一个库已经完成了所有这些工作。如果它确实存在,请参考我。

标签: pythonnumpyimage-processingcomputer-visionvision

解决方案


推荐阅读