仅检查 OpenCV 中视频源的特定部分 [英] Check only particular portion of video feed in OpenCV

查看:40
本文介绍了仅检查 OpenCV 中视频源的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to get webcam video feed, for specific width and height?

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

I have zero experience with OpenCV library, so I need help in this regard. This code is from geeksforgeeks.com. This is the only thing I have right now.

我想要实现的是,我只想在视频源的指定区域检测运动.

What I'm trying to achieve is that, I want to detect motion in only specified area of video feed.

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()

推荐答案

您似乎想要获取每一帧特定区域的感兴趣区域 (ROI).为了在 OpenCV 中做到这一点,我们可以使用边界框坐标裁剪图像.将 (0,0) 视为图像的左上角,从左到右为 x 方向,从上到下为 y 方向.如果我们将 (x1, y1) 作为 ROI 的左上顶点和 (x2,y2) 作为 ROI 的右下顶点,我们可以通过以下方式裁剪图像:

It seems like you want to obtain the region of interest (ROI) for a particular area of each frame. To do this in OpenCV, we can crop the image using bounding box coordinates. Consider (0,0) as the top left corner of the image with left-to-right as the x-direction and top-to-bottom as the y-direction. If we have (x1, y1) as the top-left vertex and (x2,y2) as the bottom-right vertex of a ROI, we can crop the image by:

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

举个例子:

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

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

We are able to do this since images are stored as a Numpy array in OpenCV. Here is a great resource for Numpy array indexing and slicing. Once you have the desired ROI, you can do your motion detecting in this region.

这篇关于仅检查 OpenCV 中视频源的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆