Skip to content

Jaccard index

Intersection over Union

Iou_stop_sign.png

이미지에서 정지 신호를 감지하는 예. 예측 경계 상자(Predicted bounding box)는 빨간색으로 그려지고 실측 경계 상자(Ground-truth bounding box)는 녹색으로 그려집니다. 우리의 목표는 이러한 경계 상자 사이의 합집합의 교차점을 계산하는 것입니다.

Iou_equation.png

합집합에 대한 교차(Intersection over Union)를 계산하는 것은 경계 상자 사이의 겹치는 영역을 합집합 영역으로 나누는 것처럼 간단합니다.

Iou_examples.png

다양한 경계 상자에 대한 합집합에 대한 교차를 계산하는 예입니다.

Mean Intersection over Union

mIoU의 "m"은 평균(Mean) 이라고 생각하면 된다. 전체 클래의 IoU를 계산하고 평균값을 계산하면 된다.

Weighted Intersection over Union

해당 항목 참조.

Python example

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

최신 (2023-08-10) 버전

# -*- coding: utf-8 -*-

from typing import Tuple, Union

NumberType = Union[int, float]
RectType = Tuple[NumberType, NumberType, NumberType, NumberType]


def calculate_iou(rect1: RectType, rect2: RectType) -> float:
    lx1, ly1, lx2, ly2 = rect1
    rx1, ry1, rx2, ry2 = rect2

    # Calculate the coordinates of the intersection rectangle
    left = max(lx1, rx1)
    top = max(ly1, ry1)
    right = min(lx2, rx2)
    bottom = min(ly2, ry2)

    # If the rectangles do not intersect, return 0
    if right <= left or bottom <= top:
        return 0.0

    # Calculate the area of intersection
    intersection_area = (right - left) * (bottom - top)

    # Calculate the area of each rectangle
    area1 = (lx2 - lx1) * (ly2 - ly1)
    area2 = (rx2 - rx1) * (ry2 - ry1)

    # Calculate the union area
    union_area = area1 + area2 - intersection_area

    # Calculate IoU
    return intersection_area / union_area

See also

Favorite site

References


  1. Vision-segmentation-miou_-_gaussian37.pdf