Intersection over Union - IoU - 交并比 (五)

Intersection over Union - IoU - 交并比 (五)

Pascal VOC Challenges:

A predicted bounding box is consideredcorrect if it overlaps more than 50% with a ground-truth bounding box, otherwisethe bounding box is considered a false positive detection. Multiple detectionsare penalized. If a system predicts several bounding boxes that overlap with asingle ground-truth bounding box, only one prediction is considered correct,the others are considered false positives.

(1 mean perfect match, 0 mean perfectmismatch)


Set the ratio type to 'Union' to computethe ratio as the area of intersection between bboxA and bboxB, divided by thearea of the union of the two.

Set the ratio type to 'Min' to compute theratio as the area of intersection between bboxA and bboxB, divided by theminimum area of the two bounding boxes.

Intersection over Union - IoU - 交并比 (五)_第1张图片

def get_iou(bb1, bb2):
    """
    Calculate the Intersection over Union (IoU) of two bounding boxes.

    Parameters
    ----------
    bb1 : dict
        Keys: {'x1', 'x2', 'y1', 'y2'}
        The (x1, y1) position is at the top left corner,
        the (x2, y2) position is at the bottom right corner
    bb2 : dict
        Keys: {'x1', 'x2', 'y1', 'y2'}
        The (x, y) position is at the top left corner,
        the (x2, y2) position is at the bottom right corner

    Returns
    -------
    float
        in [0, 1]
    """
    assert bb1['x1'] < bb1['x2']
    assert bb1['y1'] < bb1['y2']
    assert bb2['x1'] < bb2['x2']
    assert bb2['y1'] < bb2['y2']

    # determine the coordinates of the intersection rectangle
    x_left = max(bb1['x1'], bb2['x1'])
    y_top = max(bb1['y1'], bb2['y1'])
    x_right = min(bb1['x2'], bb2['x2'])
    y_bottom = min(bb1['y2'], bb2['y2'])

    if x_right < x_left or y_bottom < y_top:
        return 0.0

    # The intersection of two axis-aligned bounding boxes is always an
    # axis-aligned bounding box
    intersection_area = (x_right - x_left + 1) * (y_bottom - y_top + 1)

    # compute the area of both AABBs
    bb1_area = (bb1['x2'] - bb1['x1'] + 1) * (bb1['y2'] - bb1['y1'] + 1)
    bb2_area = (bb2['x2'] - bb2['x1'] + 1) * (bb2['y2'] - bb2['y1'] + 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 = intersection_area / float(bb1_area + bb2_area - intersection_area)
    assert iou >= 0.0
    assert iou <= 1.0
    return iou



你可能感兴趣的:(deep,learning,Darknet,-,old)