我正在用 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 区域内,现在我需要检测他是否超出范围。最后的想法是通过声音警报通知,但我主要是在那个循环内的交叉点检测上苦苦挣扎。
