cv2.VideoCapture.read()在time.sleep()之后变为旧帧 [英] cv2.VideoCapture.read() gets old frame after time.sleep()

查看:463
本文介绍了cv2.VideoCapture.read()在time.sleep()之后变为旧帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Python的opencv和两个摄像头捕获(立体声)图像,因此应每5秒保存一次图像.但是这里的问题是保存了旧框架.

I tried to capture (stereo) images with Python's opencv and two cameras so therefore every 5 seconds an image should be saved. But the problem here is that an old frame is saved.

缩小的代码如下:

    cap = cv2.VideoCapture(0)

    for i in range(20):
        time.sleep(5)
        print "Taking image %d:" % i
        ret, frame = cap.read()
        cv2.imwrite("image %d" % i, frame)
        print "  image done." if ret else "  Error while taking image..."
    cap.release()
    cv2.destroyAllWindows()

要检查这一点,我在每次拍摄图像后都更改了相机的位置.但是尽管如此,还是保存了旧位置的图像(实际上是不一样的,但是我假设最后保存的图像之后有几帧).在5张(或更多)图像之后,最终在图像中捕获的位置也确实发生了变化.

To check this, I changed the position of the camera after each taken image. But nevertheless an image from an old position is saved (actually not the same, but I assume some frames after the last saved image). After 5 (or more) images finally the captured location in the image does also change.

那么,time.sleep有什么问题吗?我想我没有得到实际的帧,而是缓冲的帧.如果是这种情况,我该如何解决它并捕获实际的帧?

So, is there any problem with time.sleep? I guess that I'm not getting the actual frame, but a buffered one. If this is the case, how could I fix it and capture the actual frame?

推荐答案

VideoCapture正在缓冲. 如果您始终需要实际的框架,请执行以下操作:

VideoCapture is buffering. If you always want the actual frame, do this:

while True:
    cap = cv2.VideoCapture(0)
    ret, frame = cap.read()
    cap.release()
    cv2.imshow(" ", frame)
    if cv2.waitKey(2000) != -1:
        break

这篇关于cv2.VideoCapture.read()在time.sleep()之后变为旧帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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