简体   繁体   中英

OpenCV - QueryFrame() returns older image from the webcam

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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