仅检查OpenCV中视频供稿的特定部分

发布于 2021-01-29 18:32:55

如何获取特定宽度和高度的网络摄像头视频提要?

我对OpenCV库零经验,因此在这方面我需要帮助。这段代码来自geeksforgeeks.com。这是我现在唯一的东西。

我想要实现的是,我只想检测视频供稿中指定区域的运动。

import cv2, time, pandas



from datetime import datetime



static_back = None
motion_list = [ None, None ] 
time = [] 
df = pandas.DataFrame(columns = ["Start", "End"]) 
video = cv2.VideoCapture(0)



while True: 
    check, frame = video.read() 
    motion = 0
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    gray = cv2.GaussianBlur(gray, (21, 21), 0)



if static_back is None: 
    static_back = gray 
    continue

diff_frame = cv2.absdiff(static_back, gray)

thresh_frame = cv2.threshold(diff_frame, 30, 255, cv2.THRESH_BINARY)[1] 
thresh_frame = cv2.dilate(thresh_frame, None, iterations = 2)

(cnts, _) = cv2.findContours(thresh_frame.copy(),  
                   cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for contour in cnts: 
    if cv2.contourArea(contour) < 50000: 
        continue
    motion = 1

    (x, y, w, h) = cv2.boundingRect(contour) 
    # making green rectangle arround the moving object 
    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

motion_list.append(motion)

motion_list = motion_list[-2:]

if motion_list[-1] == 1 and motion_list[-2] == 0: 
    time.append(datetime.now())

if motion_list[-1] == 0 and motion_list[-2] == 1: 
    time.append(datetime.now())

cv2.imshow("Gray Frame", gray)

cv2.imshow("Difference Frame", diff_frame)

cv2.imshow("Threshold Frame", thresh_frame)

cv2.imshow("Color Frame", frame)

key = cv2.waitKey(1) 
if key == ord('q'): 
    # if something is movingthen it append the end time of movement 
    if motion == 1: 
        time.append(datetime.now()) 
    break


for i in range(0, len(time), 2): 
    df = df.append({"Start":time[i], "End":time[i + 1]}, ignore_index = True)

df.to_csv("Time_of_movements.csv") 
video.release() 
cv2.destroyAllWindows()
关注者
0
被浏览
205
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    似乎您想获取每一帧特定区域的关注区域(ROI)。要在OpenCV中执行此操作,我们可以使用边界框坐标裁剪图像。将其(0,0)视为图像的左上角,从左到右作为x方向,从上到下作为y方向。如果我们具有ROI(x1, y1)的左上角和(x2,y2)右下角,则可以通过以下方式裁剪图像:

    ROI = frame[y1:y2, x1:x2]
    

    举例说明:

    -------------------------------------------
    |                                         | 
    |    (x1, y1)                             |
    |      ------------------------           |
    |      |                      |           |
    |      |                      |           | 
    |      |         ROI          |           |  
    |      |                      |           |   
    |      |                      |           |   
    |      |                      |           |       
    |      ------------------------           |   
    |                           (x2, y2)      |    
    |                                         |             
    |                                         |             
    |                                         |             
    -------------------------------------------
    

    由于图像在OpenCV中存储为Numpy数组,因此我们能够做到这一点。是Numpy数组索引和切片的重要资源。获得所需的ROI后,您就可以在该区域进行运动检测了。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看