我正在使用 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)]
现在这可行了,但令人恼火的是,它不科学且缓慢。有没有办法专门询问缓冲区中最新的图像?或者说,有更好的方法来清除缓冲区吗?
我读到在 VideoCapture 对象中有一个 5 帧缓冲区,并且有.grab
获取帧但不对其进行解码的方法。
所以你可以
cap = cv2.VideoCapture(0)
for i in xrange(4):
cap.grab()
ret, frame = cap.read()
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)