OpenCV - QueryFrame()从网络摄像头返回旧图像

问题描述:

我正在尝试使用OpenCV从网络摄像头抓取一个帧。但是QueryFrame()返回的图像比当前帧要早得多。它需要多次QueryFrame()调用才能获得最新的图像,但即使距离预期的当前图像也要滞后2到3秒。我尝试使用不同的网络摄像头,但结果是一样的。我尝试了cv2中的read()方法并遇到了同样的问题。有没有办法解决这个问题,并使用OpenCV从网络摄像头获取当前帧?

I am trying to grab a single frame from the webcam using OpenCV. But the image returned by the QueryFrame() is much older than the current frame. It takes multiple QueryFrame() calls to get the most recent image but even that lags by 2 to 3 seconds from the expected current image. I tried using different webcams but the outcomes are same. I tried the read() method from cv2 and had the same issues. Is there anyway to fix this and get the current frame from the webcam using OpenCV?

网络摄像头拥有30fps,分辨率为640/480。操作系统:Ubuntu 12.04,OpenCV 2.4.9

Webcam has 30fps with 640/480 resolution. OS : Ubuntu 12.04, OpenCV 2.4.9

# CV code
import cv
capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
cv.SaveImage("test2.JPG", img)


# CV2 code
import cv2
cam = cv2.VideoCapture()
cam.open(-1)
img=cam.read()
cv2.imwrite("test3.jpg",img[1])


解决方法对我来说问题是当我需要拍摄并立即释放它时获取网络摄像头访问权限。

A work around to fix the issue for me was to acquire the webcam access just when I needed to take a snap and releasing it immediately.

def getframe(name):  
  cam.open(0)
  img=cam.read()
  cv2.imwrite(str(name)+".jpg",img[1])
  cam.release()