openCV中waitKey函数介绍
2015-07-12 16:20
232 查看
#include <opencv2/opencv.hpp> #include < iostream > #include <window.h> using namespace cv; using namespace std; int main() { Mat im; double duration; im = imread("1.jpg"); // 测试没有namedWindow时的waitKey执行时间 duration = static_cast<double>(getTickCount()); waitKey(10000); duration = static_cast<double>(getTickCount()) - duration; duration /= getTickFrequency(); cout <<"第一个waitKey运行时间为:" << duration << 's' << endl; namedWindow("Lena"); imshow("Lena",im); // 测试有namedWindow时的waitKey执行时间 duration = static_cast<double>(getTickCount()); waitKey(10000); duration = static_cast<double>(getTickCount()) - duration; duration /= getTickFrequency(); cout <<"第二个waitKey运行时间为:" << duration << 's' << endl; return 0; }
结果:
第一个waitKey运行时间为:3.56973e-006s
第二个waitKey运行时间为:10.0054s
请按任意键继续. . .
从执行结果可以看出,第一个结果并未如期执行,第二个结果如期执行了。这是因为waitKey仅对窗口机制起作用,即namedWindow产生的窗口。若在此之前没有产生窗口,则waitKey相当于未执行。
waitKey有两个作用:
1. It waits for x milliseconds for a key press. If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns -1.
2It handles any windowing events, such as creating windows with cv::namedWindow(), or showing images with cv::imshow().
格式:
waitKey(x);
第一个参数: 等待x ms,如果在此期间有按键按下,则立即结束并返回按下按键的
ASCII码,否则返回-1
如果x=0,那么无限等待下去,直到有按键按下
如果程序想响应某个按键,可利用if(cvWaitKey(1)==Keyvalue);
经常程序里面出现if( cvWaitKey(10) >= 0 ) 是说10ms中按任意键进入此if块。
注意:这个函数是HighGUI中唯一能够获取和操作事件的函数,所以在一般的事件处理中,它需要周期地被调用,除非HighGUI被用在某些能够处理事件的环境中。比如在MFC环境下,这个函数不起作用。
相关文章推荐
- poj 1273 Drainage Ditches(最大流入门)
- Climbing Stairs
- hdu 1789 Doing Homework again(贪心)
- HDU 1532 Drainage Ditches 排水渠(最大流,入门)
- See you again(电影《速度与激情7》主题曲)
- TJU-3980Painting Tree(贪心)
- 酷融贷kurongdai来啦
- 微软官方Windows主题 英国之美2 Great Britain 高分辨率的壁纸
- 微软官方Windows主题 英国之美2 Great Britain 高分辨率的壁纸
- WIP jobs available to MRP
- macbook air 开机黑屏解决方法
- 安装rails遇到的问题
- LintCode Climbing Stairs 爬楼梯
- 【概率】Uva 10900 - So you want to be a 2n-aire?
- EF--Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法
- Svnmailer部署及应用
- AIX系统维护手册二.双机配置,HACMP
- AIX系统维护手册一.系统盘镜像
- my email is nuoyaxingzhe@sina.com
- @(报错)could not find the main class, Program will exit(已解决)