如何用Python编写案例进行交并比(IOU)计算?
- 内容介绍
- 相关推荐
本文共计705个文字,预计阅读时间需要3分钟。
计算两个矩形的交集并比较,通常在检测任务中可作为评价指标。你的预测框bbox与真实框ground truth之间的差异,可以通过计算IOU来体现。以下是一个简单的计算IOU的算法实现:
pythondef calculate_iou(bbox1, bbox2): # 矩形1和矩形2的坐标 x1_min, y1_min, x1_max, y1_max=bbox1 x2_min, y2_min, x2_max, y2_max=bbox2
# 计算两个矩形的交集区域 inter_min_x=max(x1_min, x2_min) inter_min_y=max(y1_min, y2_min) inter_max_x=min(x1_max, x2_max) inter_max_y=min(y1_max, y2_max)
# 计算交集区域的宽度和高度 inter_width=max(0, inter_max_x - inter_min_x) inter_height=max(0, inter_max_y - inter_min_y)
# 计算交集区域的面积 inter_area=inter_width * inter_height
# 计算两个矩形的并集区域 union_width=max(x1_max, x2_max) - min(x1_min, x2_min) union_height=max(y1_max, y2_max) - min(y1_min, y2_min)
# 计算并集区域的面积 union_area=union_width * union_height if union_width > 0 and union_height > 0 else 0
# 计算IOU iou=inter_area / union_area return iou
使用此函数,你可以传入两个矩形的坐标(左上角和右下角),它将返回它们的IOU值。
计算两个矩形的交并比,通常在检测任务里面可以作为一个检测指标。你的预测bbox和groundtruth之间的差异,就可以通过IOU来体现。很简单的算法实现,我也随便写了一个,嗯,很简单。
1. 使用时,请注意bbox四个数字的顺序(y0,x0,y1,x1),顺序不太一样。
#!/usr/bin/env python # encoding: utf-8 def compute_iou(rec1, rec2): """ computing IoU :param rec1: (y0, x0, y1, x1), which reflects (top, left, bottom, right) :param rec2: (y0, x0, y1, x1) :return: scala value of IoU """ # computing area of each rectangles S_rec1 = (rec1[2] - rec1[0]) * (rec1[3] - rec1[1]) S_rec2 = (rec2[2] - rec2[0]) * (rec2[3] - rec2[1]) # computing the sum_area sum_area = S_rec1 + S_rec2 # find the each edge of intersect rectangle left_line = max(rec1[1], rec2[1]) right_line = min(rec1[3], rec2[3]) top_line = max(rec1[0], rec2[0]) bottom_line = min(rec1[2], rec2[2]) # judge if there is an intersect if left_line >= right_line or top_line >= bottom_line: return 0 else: intersect = (right_line - left_line) * (bottom_line - top_line) return (intersect / (sum_area - intersect))*1.0 if __name__=='__main__': rect1 = (661, 27, 679, 47) # (top, left, bottom, right) rect2 = (662, 27, 682, 47) iou = compute_iou(rect1, rect2) print(iou)
补充知识:基于Python实现的IOU算法---最简单易懂的代码实现
概念介绍:
交并比:(Intersection over Union)
如上图所示,IOU值定位为两个矩形框面积的交集和并集的比值。即:
交并比的实现也是非常简单的,执行过程如下:
1. 交集形状的宽度计算为:
IOU_W = min(x1,x2,x3,x4)+w1+w2-max(x1,x2,x3,x4)
2. 交集形状的高度计算为:
IOU_H = min(y1,y2,y3,y4)+h1+h2-max(y1,y2,y3,y4)
其实是很简单的几何关系变换,上面的图可以帮助你很好的理解这个意思。
代码实现:001-IOU计算
以上这篇python实现IOU计算案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计705个文字,预计阅读时间需要3分钟。
计算两个矩形的交集并比较,通常在检测任务中可作为评价指标。你的预测框bbox与真实框ground truth之间的差异,可以通过计算IOU来体现。以下是一个简单的计算IOU的算法实现:
pythondef calculate_iou(bbox1, bbox2): # 矩形1和矩形2的坐标 x1_min, y1_min, x1_max, y1_max=bbox1 x2_min, y2_min, x2_max, y2_max=bbox2
# 计算两个矩形的交集区域 inter_min_x=max(x1_min, x2_min) inter_min_y=max(y1_min, y2_min) inter_max_x=min(x1_max, x2_max) inter_max_y=min(y1_max, y2_max)
# 计算交集区域的宽度和高度 inter_width=max(0, inter_max_x - inter_min_x) inter_height=max(0, inter_max_y - inter_min_y)
# 计算交集区域的面积 inter_area=inter_width * inter_height
# 计算两个矩形的并集区域 union_width=max(x1_max, x2_max) - min(x1_min, x2_min) union_height=max(y1_max, y2_max) - min(y1_min, y2_min)
# 计算并集区域的面积 union_area=union_width * union_height if union_width > 0 and union_height > 0 else 0
# 计算IOU iou=inter_area / union_area return iou
使用此函数,你可以传入两个矩形的坐标(左上角和右下角),它将返回它们的IOU值。
计算两个矩形的交并比,通常在检测任务里面可以作为一个检测指标。你的预测bbox和groundtruth之间的差异,就可以通过IOU来体现。很简单的算法实现,我也随便写了一个,嗯,很简单。
1. 使用时,请注意bbox四个数字的顺序(y0,x0,y1,x1),顺序不太一样。
#!/usr/bin/env python # encoding: utf-8 def compute_iou(rec1, rec2): """ computing IoU :param rec1: (y0, x0, y1, x1), which reflects (top, left, bottom, right) :param rec2: (y0, x0, y1, x1) :return: scala value of IoU """ # computing area of each rectangles S_rec1 = (rec1[2] - rec1[0]) * (rec1[3] - rec1[1]) S_rec2 = (rec2[2] - rec2[0]) * (rec2[3] - rec2[1]) # computing the sum_area sum_area = S_rec1 + S_rec2 # find the each edge of intersect rectangle left_line = max(rec1[1], rec2[1]) right_line = min(rec1[3], rec2[3]) top_line = max(rec1[0], rec2[0]) bottom_line = min(rec1[2], rec2[2]) # judge if there is an intersect if left_line >= right_line or top_line >= bottom_line: return 0 else: intersect = (right_line - left_line) * (bottom_line - top_line) return (intersect / (sum_area - intersect))*1.0 if __name__=='__main__': rect1 = (661, 27, 679, 47) # (top, left, bottom, right) rect2 = (662, 27, 682, 47) iou = compute_iou(rect1, rect2) print(iou)
补充知识:基于Python实现的IOU算法---最简单易懂的代码实现
概念介绍:
交并比:(Intersection over Union)
如上图所示,IOU值定位为两个矩形框面积的交集和并集的比值。即:
交并比的实现也是非常简单的,执行过程如下:
1. 交集形状的宽度计算为:
IOU_W = min(x1,x2,x3,x4)+w1+w2-max(x1,x2,x3,x4)
2. 交集形状的高度计算为:
IOU_H = min(y1,y2,y3,y4)+h1+h2-max(y1,y2,y3,y4)
其实是很简单的几何关系变换,上面的图可以帮助你很好的理解这个意思。
代码实现:001-IOU计算
以上这篇python实现IOU计算案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

