【发布时间】:2016-12-31 22:16:26
【问题描述】:
我正在使用 OpenCV2 使用网络摄像头拍摄一些延时摄影照片。我想提取网络摄像头看到的最新视图。我尝试这样做。
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)]
现在这可行,但它非常不科学且速度慢。有没有办法专门询问缓冲区中的最新图像?还是说,有更好的方法来清除缓冲区?
【问题讨论】: