【问题标题】:How to match values from two separate lists based on a condition?如何根据条件匹配来自两个单独列表的值?
【发布时间】:2023-02-03 22:43:18
【问题描述】:

我有两个包含图像边界框信息的列表,如下所示:

Image_1 = [(10,5,2,8),(1,5,9,5),(6,1,8,0)...]
Image_2 = [(11,4,1,7),(1,6,10,6),(6,1,9,1)...]

Image_1 中的值是图像中对象的真实边界框位置,Image_2 中的值是 OCR 程序输出的边界框。我需要将 Image_1 数组中的边界框与 Image_2 数组中最接近的匹配项进行匹配。由于值会略有不同,我使用一个函数来返回并集计算的交集,如下所示:

def bb_intersection_over_union(boxA, boxB):
    # determine the (x, y)-coordinates of the intersection rectangle
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])
    # compute the area of intersection rectangle
    interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
    # compute the area of both the prediction and ground-truth
    # rectangles
    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
    # compute the intersection over union by taking the intersection
    # area and dividing it by the sum of prediction + ground-truth
    # areas - the interesection area
    iou = interArea / float(boxAArea + boxBArea - interArea)
    # return the intersection over union value
    return iou 

最大的 IOU 值表示最接近的匹配。如何遍历 Image_1 和 Image_2 数组并匹配值?

【问题讨论】:

    标签: python loops set-intersection


    【解决方案1】:

    您可以使用嵌套的 for 循环遍历两个数组,并使用 bb_intersection_over_union 函数计算每对边界框之间的 IOU。您可以跟踪 Image_1 中每个边界框的最佳匹配,并存储具有最高 IOU 的一对边界框。这是 Python 中的示例实现:

    def match_bounding_boxes(image1, image2):
        matches = []
        for box1 in image1:
            best_iou = 0
            best_box = None
            for box2 in image2:
                iou = bb_intersection_over_union(box1, box2)
                if iou > best_iou:
                    best_iou = iou
                    best_box = box2
            matches.append((box1, best_box))
        return matches
    

    此函数将 image1 和 image2 作为输入,并返回一个元组列表,每个元组包含 image1 和 image2 中具有最高 IOU 的匹配边界框对。

    【讨论】:

      猜你喜欢
      • 2021-02-23
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      相关资源
      最近更新 更多