OpenCV - 如何检测两个矩形之间的相交?

3

我正在使用OpenCV实现一种报警系统。我需要在“安全区域”内跟踪一个人,检测并通知是否越界。

我已经实现了运动跟踪。现在我需要定义ROI(地板上的安全区域矩形;摄像头在天花板上),并检测它和人体边框矩形之间的交集。

类似于 这个

我有以下内容:

while True:
 
    # Grab frame from webcam
    ret, color_frame = vid.read()

    # resize the frame, convert it to grayscale, and blur it
    color_frame = imutils.resize(color_frame, width=500)
    gray = cv2.cvtColor(color_frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)

    # MOTION TRACKING #
    # Defining safe zone area on the floor.
    roi = cv2.rectangle(color_frame, (50, 50), (500, 500), (0, 0, 0), 2)

    # First frame to compare motion
    if firstFrame is None:
        firstFrame = gray
        continue

    # Absolute difference to detect changes.
    frameDelta = cv2.absdiff(firstFrame, gray)
    thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]

    # Finding contours
    thresh = cv2.dilate(thresh, None, iterations=2)
    contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(contours)

    # Filtering contours
    for c in contours:
        # if the contour is too small, ignore it
        if cv2.contourArea(c) < 5000:
            continue

        # Bounding Rect for the person which we are tracking
        (x, y, w, h) = cv2.boundingRect(c)
        boudingRect = cv2.rectangle(color_frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

如我所说,这个人在ROI区域内,现在我需要检测他是否越界。 最终的想法是通过警报声来通知,但我主要遇到的问题是在循环内进行交叉检测。


1
安全区似乎是矩形(50, 50), (550, 550),对吗?那么,简单地检查x <= 50 or y <= 50 or (x+w) >= 550 or (y+h) >= 550就可以解决问题了,对吧!? - HansHirse
1个回答

5

本文将帮助您了解如何使用cv2.pointPolygonTest实现两个矩形之间的交集。关于pointPolygonTest的更多信息,请查看此处

import cv2
import numpy as np


def check_intersection(polygon1, polygon2):
    intersection = False
    for point in polygon2:
        result = cv2.pointPolygonTest(polygon1, tuple(point), measureDist=False)
        # if point inside return 1
        # if point outside return -1
        # if point on the contour return 0

        if result == 1:
            intersection = True

    return intersection


image1 = np.zeros((300, 300), dtype=np.uint8)
image2 = np.zeros((300, 300), dtype=np.uint8)

# take 4 points of
rectangle1_cordinates = np.array([[50, 50], [50, 100], [100, 100], [100, 50]])
rectangle2_cordinates = np.array([[75, 75], [75, 125], [125, 125], [125, 75]])
rectangle3_cordinates = np.array([[110, 110], [110, 160], [160, 160], [160, 110]])

result_1_and_2 = check_intersection(rectangle1_cordinates, rectangle2_cordinates)
result_1_and_3 = check_intersection(rectangle1_cordinates, rectangle3_cordinates)

cv2.putText(image1, str(result_1_and_2), (20, 20), cv2.FONT_HERSHEY_COMPLEX, 0.8, 255, 1)
cv2.drawContours(image1, [rectangle1_cordinates], -1, 255, 2)
cv2.drawContours(image1, [rectangle2_cordinates], -1, 255, 2)

cv2.putText(image2, str(result_1_and_3), (20, 20), cv2.FONT_HERSHEY_COMPLEX, 0.8, 255, 1)
cv2.drawContours(image2, [rectangle1_cordinates], -1, 255, 2)
cv2.drawContours(image2, [rectangle3_cordinates], -1, 255, 2)

cv2.imshow("image1", image1)
cv2.imshow("image2", image2)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

输入图像描述


是的,但我正在苦苦思索如何使用cv2矩形来实现,并在for循环内定义BoundingRect。 - Javier Alonso Delgado
cv2.boundingRect的情况下,您有矩形左上角坐标(x,y)和高度和宽度(w,h)。使用这些信息,您可以创建所有4个坐标。左上角:(x,y),右上角:(x + w,y),右下角:(x + w,y + h),左下角:(x,y + h)。 - Vatsal Parsaniya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接