def computeIOU(local_xml_info_list, need_check_info): xmin1, ymin1, xmax1, ymax1 = local_xml_info_list xmin2, ymin2, xmax2, ymax2 = need_check_info # box1 = [xmin1, ymin1, xmax1, ymax1] #左上角的坐标与右下角的坐标 # box2 = [xmin2, ymin2, xmax2, ymax2] # # iou_ratio--交并比 # width1 = abs(box1[2] - box1[0]) # height1 = abs(box1[1] - box1[3]) # 这里y1-y2是因为一般情况y1>y2,为了方便采用绝对值 # width2 = abs(box2[2] - box2[0]) # height2 = abs(box2[1] - box2[3]) # x_max = max(box1[0],box1[2],box2[0],box2[2]) # y_max = max(box1[1],box1[3],box2[1],box2[3]) # x_min = min(box1[0],box1[2],box2[0],box2[2]) # y_min = min(box1[1],box1[3],box2[1],box2[3]) # iou_width = x_min + width1 + width2 - x_max # iou_height = y_min + height1 + height2 - y_max # if iou_width <= 0 or iou_height <= 0: # iou_ratio = 0 # else: # iou_area = iou_width * iou_height # 交集的面积 # box1_area = width1 * height1 # box2_area = width2 * height2 # iou_ratio = iou_area / (box1_area + box2_area - iou_area) # 并集的面积 # return iou_ratio # 求交集部分左上角的点 xmin = max(xmin1, xmin2) ymin = max(ymin1, ymin2) # 求交集部分右下角的点 xmax = min(xmax1, xmax2) ymax = min(ymax1, ymax2) # 计算输入的两个矩形的面积 s1 = (xmax1 - xmin1) * (ymax1 - ymin1) s2 = (xmax2 - xmin2) * (ymax2 - ymin2) # 计算总面积 s = s1 + s2 # 计算交集 inter_area = (xmax - xmin) * (ymax - ymin) iou = inter_area / (s - inter_area)
转载及参考:
https://blog.csdn.net/lmxt520/article/details/98473024
https://blog.csdn.net/mndlgzzd/article/details/107604446