我有此代码:
import cv2
from matplotlib import pyplot as plt
import numpy as np
img = cv2.imread('forklift2.jpg')
A = cv2.rectangle(img, (180, 90), (352, 275), (255,0,0), 2)
B = cv2.rectangle(img, (100, 220), (300, 275), (155,122,100), 2)
cv2.imshow('Object detector', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
我需要检测两个矩形A和B之间的交点,就像图片中显示的那样:
所以我需要一个布尔变量,如果2个矩形具有某些公共区域,则该布尔变量应为true。 我怎样才能做到这一点?
答案 0 :(得分:0)
您可以按以下方式引用IOU
工具:
def pre_IOU(Reframe, GTframe):
"""
input is rect diagonal points
"""
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
return 0
else:
Area = width*height # two box cross
Area1 = width1*height1
Area2 = width2*height2
ratio = Area*1./(Area1+Area2-Area)
return Area