cvWaitKey()是opencv中经常用到而且很好用的一个函数
函数定义
CV_EXPORTS_W int waitKey(int delay=0);
Highgui_c.h中
/* wait for key event infinitely (delay<=0) or for "delay" milliseconds */ CVAPI(int) cvWaitKey(int delay CV_DEFAULT(0));
waitkey灵活运用注意如下几点
1.当参数delay中为负,则无穷等待
2.否则等待delay ms.
3.返回值是按键值 否则返回-1
4.当参数为空,则一直等待按键当前线程等待
5.waitkey只对显示图像窗口有效,对控制台无效
例程:
// mOpencv.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "tools.h" int _tmain(int argc, _TCHAR* argv[]) { IplImage* src = 0; int key = 0; CvCapture* capture = cvCaptureFromCAM(0); while( NULL != (src = cvQueryFrame(capture) ) ) { cvShowImage("camera",src); if( (key = waitKey(2)) !=-1)//如果2ms内有按键 有返回值key { printf("press key:%c\n",key); if(key == 'q') break; } } //此函数等待按键,按键盘任意键就返回 waitKey(); if(NULL != src) cvReleaseImage(&src); if(NULL != capture) cvReleaseCapture(&capture); return 0; }
附上资料:
http://opencv.willowgarage.com/wiki/documentation/c/highgui/WaitKey