C++ OpenCV 的 cvWaitKey() 函数有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5217519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does OpenCV's cvWaitKey( ) function do?
提问by Simplicity
What happens during the execution of cvWaitKey()
? What are some typical use cases? I saw it in OpenCVreference but the documentation isn't clear on its exact purpose.
执行过程中会发生什么cvWaitKey()
?有哪些典型的用例?我在OpenCV参考中看到了它,但文档并不清楚其确切用途。
回答by SuperElectric
cvWaitKey(x) / cv::waitKey(x)
does two things:
cvWaitKey(x) / cv::waitKey(x)
做两件事:
- It waits for xmilliseconds for a key press on a OpenCV window (i.e. created from
cv::imshow()
). Note that it does not listen on stdin for console input. If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns-1
. (If xis zero, it waits indefinitely for the key press.) - It handles any windowing events, such as creating windows with
cv::namedWindow()
, or showing images withcv::imshow()
.
- 它等待x毫秒在 OpenCV 窗口(即从 中创建
cv::imshow()
)上的按键。请注意,它不会在 stdin 上侦听控制台输入。如果在此期间按下某个键,则返回该键的 ASCII 代码。否则,它返回-1
。(如果x为零,它会无限期地等待按键。) - 它处理任何窗口事件,例如
cv::namedWindow()
使用cv::imshow()
.
A common mistake for opencv newcomers is to call cv::imshow()
in a loop through video frames, without following up each draw with cv::waitKey(30)
. In this case, nothing appears on screen, because highgui is never given time to process the draw requests from cv::imshow()
.
opencv 新手的一个常见错误是cv::imshow()
通过视频帧循环调用,而没有使用cv::waitKey(30)
. 在这种情况下,屏幕上不会显示任何内容,因为 highgui 从来没有时间处理来自cv::imshow()
.
回答by karlphillip
Plain simply, cvWaitKey()
sleeps for Xmiliseconds, waiting for any key to be pressed.
简单地说,cvWaitKey()
睡眠X毫秒,等待按下任何键。
int cvWaitKey(int X);
If a key is pressed, this function returns the ASCII code of key. Or returns -1if no keys were pressed during that time.
如果按键被按下,该函数返回按键的 ASCII 码。如果在此期间没有按下任何键,则返回-1。
回答by Jorge Vega Sánchez
cvWaitKey(0)
stops your program until you press a button.
cvWaitKey(0)
停止您的程序,直到您按下按钮。
cvWaitKey(10)
doesn't stop your program but wake up and alert to end your program when you press a button. Its used into loops because cvWaitkey
doesn't stop loop.
cvWaitKey(10)
不会停止您的程序,而是在您按下按钮时唤醒并提醒结束您的程序。它用于循环,因为cvWaitkey
不会停止循环。
Normal use
正常使用
char k;
k=cvWaitKey(0);
if(k == 'ESC')
with k
you can see what key was pressed.
与k
你所看到的按下了哪个键。
回答by Jorge Vega Sánchez
. argument of 0
is interpreted as infinite
. 的参数0
被解释为infinite
. in order to drag the highGUI windows, you need to continually call the cv::waitKey()
function. eg for static images:
. 为了拖动highGUI窗口,您需要不断调用该cv::waitKey()
函数。例如对于静态图像:
cv::imshow("winname", img);
cv::imshow("winname", img);
while(cv::waitKey(1) != 27); // 27 = ascii value of ESC
while(cv::waitKey(1) != 27); // 27 = ascii value of ESC
回答by Andy
Note for anybody who may have had problems with the cvWaitKey( )
function. If you are finding that cvWaitKey(x)
is not waiting at all, make sure you actually have a window open (i.e. cvNamedWindow(...)
). Put the cvNamedWindow(...)
declaration BEFORE any cvWaitKey()
function calls.
请注意可能对该cvWaitKey( )
功能有问题的任何人。如果您发现cvWaitKey(x)
根本没有等待,请确保您确实打开了一个窗口(即cvNamedWindow(...)
)。将cvNamedWindow(...)
声明放在任何cvWaitKey()
函数调用之前。
回答by enthusiasticgeek
/* Assuming this is a while loop -> e.g. video stream where img is obtained from say web camera.*/
cvShowImage("Window",img);
/* A small interval of 10 milliseconds. This may be necessary to display the image correctly */
cvWaitKey(10);
/* to wait until user feeds keyboard input replace with cvWaitKey(0); */
回答by sue-ling
The cvWaitKey
simply provides something of a delay. For example:
该cvWaitKey
只是提供了一个延迟的东西。例如:
char c = cvWaitKey(33);
if( c == 27 ) break;
Tis was apart of my code in which a video was loaded into openCV and the frames outputted. The 33
number in the code means that after 33ms
, a new frame would be shown. Hence, the was a dely or time interval of 33ms
between each frame being shown on the screen.
Hope this helps.
这是我将视频加载到 openCV 并输出帧的代码的一部分。33
代码中的数字表示在 之后33ms
,将显示一个新框架。因此,33ms
在屏幕上显示的每一帧之间有一个延迟或时间间隔。希望这可以帮助。
回答by Pravin Dahale
cvWaitKey(milliseconds)
simply wait for milliseconds provided as a parameter for a next key stroke of keyboard.
cvWaitKey(milliseconds)
只需等待作为下一个键盘击键参数提供的毫秒。
Human eyes not able to see the thing moving in less than 1/10 second, so we use this to hold same image frame for some time on screen. As soon as the key of keyboard is pressed the next operation will be perform.
人眼无法在 1/10 秒内看到移动的物体,因此我们使用它在屏幕上保持相同的图像帧一段时间。只要按下键盘上的键,就会执行下一个操作。
In short cvWaitKey(milliseconds)
wait either for key press or millisecond
time provided.
简而言之,cvWaitKey(milliseconds)
等待按键或millisecond
提供的时间。
回答by Yassine Abdul-Rahman
waits milliseconds to check if the key is pressed, if pressed in that interval return its ascii value, otherwise it still -1
等待毫秒以检查键是否被按下,如果在该间隔内按下,则返回其 ascii 值,否则仍为 -1