目标检测中iou的计算(python代码)

# 计算两矩形IOU值, 输入为两矩形对角线(x,y)坐标
def IOU(Reframe, GTframe):
	# 得到第一个矩形的左上坐标及宽和高
	x1 = Reframe[0]
	y1 = Reframe[1]
	width1 = Reframe[2] - Reframe[0]
	height1 = Reframe[3] - Reframe[1]

	# 得到第二个矩形的左上坐标及宽和高
	x2 = GTframe[0]
	y2 = GTframe[1]
	width2 = GTframe[2] - GTframe[0]
	height2 = GTframe[3] - GTframe[1]

	# 计算重叠部分的宽和高
	endx = max(x1+width1, x2+width2)
	startx = min(x1, x2)
	width = width1 + width2 - (endx - startx)
	
	endy = max(y1+height1, y2+height2)
	starty = min(y1, y2)
	height = height1 + height2 - (endy - starty)

	# 如果重叠部分为负, 即不重叠
	if width <= 0 or height <= 0:
		ratio = 0
	else:
		Area = width * height
		Area1 = width1 * height1
		Area2 = width2 * height2
		ratio = Area*1. / (Area1+Area2-Area)

	return ratio

猜你喜欢

转载自blog.csdn.net/weixin_42561002/article/details/87914497