我尝试使用 Python 的 opencv
和两个摄像头捕捉(立体)图像,因此每 5 秒应保存一张图像。但这里的问题是保存了一个旧框架。
压缩后的代码如下:
cap = cv2.VideoCapture(0)
for i in range(20):
time.sleep(5)
print "Taking image %d:" % i
ret, frame = cap.read()
cv2.imwrite("image %d" % i, frame)
print " image done." if ret else " Error while taking image..."
cap.release()
cv2.destroyAllWindows()
为了检查这一点,我在每次拍摄图像后更改相机的位置。但是仍然保存了旧位置的图像(实际上不一样,但我假设在最后保存的图像之后有一些帧)。在 5 张(或更多)图像之后,图像中的捕获位置最终也会发生变化。
那么,time.sleep
有什么问题吗?我想我得到的不是实际帧,而是缓冲帧。如果是这种情况,我该如何修复它并捕获实际帧?
最佳答案
VideoCapture 正在缓冲。 如果您总是想要实际的框架,请执行以下操作:
while True:
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cap.release()
cv2.imshow(" ", frame)
if cv2.waitKey(2000) != -1:
break
关于python - cv2.VideoCapture.read() 在 time.sleep() 之后获取旧帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34656304/