OPENCV3 waitKey函数使用笔记

源码说明:

CV_EXPORTS_W int waitKey(int delay = 0);

@param delay Delay in milliseconds. 0 is the special value that means "forever".

The function waitKey waits for a key event infinitely (when \f$\texttt{delay}\leq 0\f$ ) or for delay
milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the
function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is
running on your computer at that time. It returns the code of the pressed key or -1 if no key was
pressed before the specified time had elapsed.


@note
This function is the only method in HighGUI that can fetch and handle events, so it needs to be
called periodically for normal event processing unless HighGUI is used within an environment that
takes care of event processing.


@note
The function only works if there is at least one HighGUI window created and the window is active.

If there are several HighGUI windows, any of them can be active.

WaitKey()函数的功能是是程序暂停,等待用户触发一个按键操作。

1.如果该函数参数设为一个正数,则程序将暂停一段时间,时间长为该整数值个毫秒单位,如果在DELAY时间内用户按下按键,就会返回这个按键的按键值。如果在这段时间内用户没有按下按键就返回-1,之后继续执行之后的程序,这个时候就相当于一个延时函数。

2.如果设置waitKey(0),则表示程序会一直的等待用户的按键事件

使用实例:

1.while(1){ if(waitKey(100)==27)break; } 在这个程序中,我们告诉OpenCv等待用户触发事件,等待时间为100ms,如果在这个时间段内, 用户按下ESC(ASCII码为27),则跳出循环,否则,则跳出循环

扫描二维码关注公众号,回复: 1661079 查看本文章

2.显示图像,一般要在ShowImage()函数后加一句WaitKey(0),此时程序显示出图像后将暂停,等待接收一个键盘输入;若没有这句话,则显示图像的代码很快就执行过去了,所以要用WaitKey()来暂停

3.根据资料的说明,在显示图像的时候,每秒显示27、28帧的时候,我们看到的视频是流畅的。假如采取25帧的话,k=1000/25=40。即,每处理完一帧后,程序会等待40毫秒才会读取下一帧;显示视频时,一般用WaitKey(delay) 。表示显示一帧,然后等delay ms ,再显示下一帧。如果没有cvWaitKey的话, 那么循环里面的每个指令执行时间为0.0000000001s,总之趋近于0.那么capture可能有10000帧图像, 那么10000帧图像也之需要0.000001s就播放完了, 结果你还没看到画面,就已经黑屏了 ..

4.如果程序想响应某个按键,可利用if(cvWaitKey(1)==Keyvalue);

5.经常程序里面出现if( WaitKey(10) >= 0 ) 是说10ms中按任意键退出

参考链接:

http://www.360doc.com/content/16/0928/20/25664332_594471048.shtml

猜你喜欢

转载自blog.csdn.net/wwwlyj123321/article/details/80455038