IoU

本文图像内容均来自这里,如果您对cv和dl感兴趣,请查看此连接获取详细介绍。 目标检测中给定图片输出候选框,这些候选框通常称为IoU。为了使用IoU评估需要的目标检测,需要:

  • ground-truth bounding boxes(真实框)(手动标记的框的位置)
  • predicted bound boxes(模型的预测框) 上图中手工标记的框的颜色为绿色,预测框输出颜色为红色。 通过下图计算IoU 上面计算公式中Aria of Union表示两个框面积的并,Area of Overlap表示两个框的重叠区域。 ``` from collections import namedtuple import numpy as np import cv2 Detection = namedtuple("Detection",["image_path","gt","pred"])

    计算IoU

    def bb_intersection_over_union(boxA,boxB):

    获取公共区域的左上角坐标和右下角坐标

    xA = max(boxA[0],boxB[0]) yA = max(boxA[1],boxB[1]) xB = min(boxA[2],boxB[2]) yB = min(boxA[3],boxB[3]) interArea = (xB-xA+1)*(yB-yA+1)

    计算真实框面积和预测框面积

    boxAArea = (boxA[2]-boxA[0]+1)(boxA[3]-boxA[1]+1) boxBArea = (boxB[2]-boxB[0]+1)(boxB[3]-boxB[1]+1)

    计算重叠部分面积

    interArea = (xB-xA+1)(yB-yA+1) boxAArea = (boxA[2]-boxA[0]+1)(boxA[3]-boxA[1]+1) boxBArea = (boxB[2]-boxB[0]+1)*(boxB[3]-boxB[1]+1) iou = interArea/float(boxAArea+boxBArea-interArea) return iou

    开始测试IoU

    examples = [ Detection("image_1.png", [239, 81, 454, 303], [240, 90, 500, 250]), Detection("image_2.png", [176, 118, 386, 328], [150, 120, 390, 350]), Detection("image_3.png", [226, 124, 483, 384], [330, 130, 500, 350])] for detection in examples: image = cv2.imread(detection.image_path) cv2.rectangle(image,tuple(detection.gt[:2]),tuple(detection.gt[2:]),(0,255,0),3) cv2.rectangle(image,tuple(detection.pred[:2]),
                 tuple(detection.pred[2:]),(0,0,255),2)
    
    iou = bb_intersection_over_union(detection.gt,detection.pred) cv2.putText(image,"IoU:{:.4f}".format(iou),(10,30),
             cv2.FONT_HERSHEY_SIMPLEX,0.6,(0,255,0),2)
    
    print("{}:{:.4f}".format(detection.image_path,iou)) cv2.imshow("Image",image) k = cv2.waitKey(0) if k == 27:
      continue
    
    cv2.destroyAllWindows()

```

results matching ""

    No results matching ""