简体   繁体   English

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

[英]OpenCV - QueryFrame() returns older image from the webcam

I am trying to grab a single frame from the webcam using OpenCV. 我试图使用OpenCV从网络摄像头抓取一个帧。 But the image returned by the QueryFrame() is much older than the current frame. 但是QueryFrame()返回的图像比当前帧要早得多。 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. 它需要多次QueryFrame()调用才能获得最新的图像,但即使距离预期的当前图像也要滞后2到3秒。 I tried using different webcams but the outcomes are same. 我尝试使用不同的网络摄像头,但结果是相同的。 I tried the read() method from cv2 and had the same issues. 我尝试了cv2中的read()方法并遇到了同样的问题。 Is there anyway to fix this and get the current frame from the webcam using OpenCV? 有没有办法解决这个问题,并使用OpenCV从网络摄像头获取当前帧?

Webcam has 30fps with 640/480 resolution. 网络摄像头具有30fps,640/480分辨率。 OS : Ubuntu 12.04, OpenCV 2.4.9 操作系统: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() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM