Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x OpenCV库的VideoCapture方法(在Python中)是处理每一帧,还是在进行大量处理时也跳过帧?_Python 3.x_Opencv_Video Capture - Fatal编程技术网

Python 3.x OpenCV库的VideoCapture方法(在Python中)是处理每一帧,还是在进行大量处理时也跳过帧?

Python 3.x OpenCV库的VideoCapture方法(在Python中)是处理每一帧,还是在进行大量处理时也跳过帧?,python-3.x,opencv,video-capture,Python 3.x,Opencv,Video Capture,我正在使用Python中的OpenCV库来读取实时视频帧,以便跟踪每个帧中的多个对象 我使用VideoCapture方法进行此操作,代码如下所示: vid = cv2.VideoCapture() # Loop over all frames while True: ok, frame = vid.read() if not ok: break # Quite heavy computations 所以我得到了每隔一个while循环,VideoCa

我正在使用Python中的OpenCV库来读取实时视频帧,以便跟踪每个帧中的多个对象

我使用VideoCapture方法进行此操作,代码如下所示:

vid = cv2.VideoCapture()

# Loop over all frames
while True:

    ok, frame = vid.read()
    if not ok:
       break

    # Quite heavy computations


所以我得到了每隔一个while循环,
VideoCapture
调用
read()
方法来处理一帧。但是,我想知道在处理这个帧的过程中会发生什么?我的猜测是,在此处理过程中跳过了许多帧。这是真的还是帧被添加到缓冲区,并且最终都会按顺序处理?

假设您没有从文件中读取,相机的帧将被添加到预定义大小的缓冲区。你可以通过

cv2.get(cv2.CAP_PROP_BUFFERSIZE)
并着手

cv2.set(cv2.CAP_PROP_BUFFERSIZE, my_size)

填充缓冲区后,将跳过新帧

即使
VideoCapture
有一个缓冲区来存储图像,在一个繁重的过程中,你的循环会跳过一些帧。根据标准,您的
VideoCaptureProperties
具有属性
CAP\u PROP\u BUFFERSIZE=38
,这意味着它将存储38帧。该方法使用从缓冲区读取下一帧的方法

您可以自己测试它,下面是一个简单的示例,它带有一个时间延迟来模拟一个繁重的过程

import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)

    # Introduce a delay to simulate heavy process
    time.sleep(1) 
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

您将看到图像跳过帧(并且不会产生我们在慢速图像序列中预期的“慢动作”效果)。因此,如果处理速度足够快,则可以匹配摄影机FPS

但这是否意味着,如果该过程计算量过大,实际处理将开始缓慢落后于摄像机输入?或者缓冲区是否保持最新,这意味着每次循环时,进程都会捕获一个最新的相机帧?