赞
踩
- import cv2
- def IOU(Reframe, GTframe):
- # 得到第一个矩形的左上坐标及宽和高
- x1 = Reframe[0]
- y1 = Reframe[1]
- width1 = Reframe[2]
- height1 = Reframe[3]
- # 得到第二个矩形的左上坐标及宽和高
- x2 = GTframe[0]
- y2 = GTframe[1]
- width2 = GTframe[2]
- height2 = GTframe[3]
-
-
- # 计算重叠部分的宽和高
- 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.0 / (Area1 + Area2 - Area)
-
- return ratio
-
-
-
- def draw(left_up, right_down, color, th):
- # print(left_up, right_down)
- # 要画在哪张图片上
- img = cv2.imread('1.jpg')
- # print(img.shape)
- cv2.rectangle(img, left_up, right_down, color, th)
- cv2.imshow("fff", img)
- # 这个是一直显示,阻塞在屏幕上。比较好调试,看效果
- # k = cv2.waitKey(0)
- # 将画后的图片继续覆盖到原图
- cv2.imwrite('1.jpg', img)
-
- # 计算IOU值
- rec_du = (53,50,100,110) # (x, y , width, height)
- rec_s = (40,70,80,190) # (x, y , width, height)
- iou_res = IOU(rec_du, rec_s)
- print(iou_res)
-
- # 绘图
- # 分别传入左上顶点和右下顶点坐标,(0, 0, 255)代表颜色,rgb通道的,2是代表画出来的线的厚度
- draw((53,50), (100,110), (0, 0, 255), 2)
- draw((40,70), (80,190), (0, 0, 255), 2)
- # draw(left_up, right_down, (0, 0, 255), 2)
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。