我正在使用OpenCV2使用摄像头拍摄一些timelapse照片。我想提取摄像头看到的最近的视图。我试图做到这一点。从网络摄像头获取最新帧
import cv2
a = cv2.VideoCapture(1)
ret, frame = a.read()
#The following garbage just shows the image and waits for a key press
#Put something in front of the webcam and then press a key
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]
#Since something was placed in front of the webcam we naively expect
#to see it when we read in the next image. We would be wrong.
ret, frame = a.read()
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]
除了放置在网络摄像头前面的图像没有显示。这几乎就像如果有某种缓冲......
所以我清除该缓冲区,就像这样:
import cv2
a = cv2.VideoCapture(1)
ret, frame = a.read()
#Place something in front of the webcam and then press a key
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]
#Purge the buffer
for i in range(10): #Annoyingly arbitrary constant
a.grab()
#Get the next frame. Joy!
ret, frame = a.read()
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]
现在这个工作,但它是烦人不科学的和缓慢的。有没有办法只专门询问缓冲区中最新的图像?或者说,更好的方法来清除缓冲区?
它可能没有一个缓冲的问题,因为它是不断清空缓冲区。缓冲问题对我来说是显而易见的,因为图像捕获之间存在延迟。 – Richard