本文主要是介绍深度学习_目标检测_IOU(交并比)Python实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在图像里,目标检测是其中一个领域,最著名的是以矩形框作为目标检测区域,在图像中表达一个矩形框需要有坐标(top,left,bottom,right),即左上角坐标,右下角坐标。从而可以在给定的两个矩形中计算IOU值(交并比),小数点可以四舍五入保留4位。
例如输入:
(661,27,679,47)
(662,27,682,47)
接下来我们用Python来进行编程:
def compute_iou(rect1,rect2):# (y0,x0,y1,x1) = (top,left,bottom,right)S_rect1 = (rect1[2] - rect1[0]) * (rect1[3] - rect1[1])S_rect2 = (rect2[2] - rect2[0]) * (rect2[3] - rect1[1])sum_all = S_rect1 + S_rect2left_line = max(rect1[1],rect2[1])right_line = min(rect1[3],rect2[3])top_line = max(rect1[0],rect2[0])bottom_line = min(rect1[2],rect2[2])if left_line >= right_line or top_line >= bottom_line:return 0else:intersect = (right_line - left_line) * (bottom_line - top_line)return (intersect / (sum_area - intersect)) * 1.0if__name__=='__main__':a,b,c,d,e,f,g,h = map(int, input().split())rect1 = (a,b,c,d)rect2 = (e,f,g,h)iou = compute_iou(rect1,rect2)print("%.4f" %iou)
这篇关于深度学习_目标检测_IOU(交并比)Python实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!