问题描述
我刚刚开始使用 Python 的 OpenCV 库,但遇到了一些我不明白的事情.
I've just begun using the OpenCV library for Python and came across something I didn't understand.
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read() #returns ret and the frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
当我使用 cv2.waitKey(1)
时,我从我的笔记本电脑网络摄像头获得连续的实时视频源.但是,当我使用 cv2.waitKey(0)
时,我会得到静止图像.每次关闭窗口时,都会弹出另一个窗口,其中包含当时拍摄的另一张照片.为什么它不显示为连续供稿?
When I use cv2.waitKey(1)
, I get a continuous live video feed from my laptops webcam. However when I use cv2.waitKey(0)
, I get still images. Every time I close the window, another one pops up with another picture taken at the time.Why does it not show as a continuous feed?
推荐答案
来自 文档:
1.waitKey(0)
将无限显示窗口,直到任何按键(适合图像显示).
1.waitKey(0)
will display the window infinitely until any keypress (it is suitable for image display).
2.waitKey(1)
将显示一帧 1
ms,之后显示将自动关闭.由于操作系统在切换线程之间有最短时间,因此该函数不会完全等待 1
毫秒,它会等待至少 1
毫秒,具体取决于您的系统上正在运行的其他内容当时的电脑.
2.waitKey(1)
will display a frame for 1
ms, after which display will be automatically closed. Since the OS has a minimum time between switching threads, the function will not wait exactly 1
ms, it will wait at least 1
ms, depending on what else is running on your computer at that time.
因此,如果您使用 waitKey(0)
,您会看到一个静止图像,直到您实际按下某些东西,而 waitKey(1)
函数将显示一个帧至少 1
毫秒.
So, if you use waitKey(0)
you see a still image until you actually press something while for waitKey(1)
the function will show a frame for at least 1
ms only.
这篇关于waitKey(0) 和 waitKey(1) 的输出差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!