我正在使用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