OpenCV 的 cvWaitKey()函数是做什么的?

在执行 cvWaitKey()期间会发生什么?一些典型的用例是什么?我在 OpenCV的参考文献中看到了它,但是文档中并没有明确说明它的确切用途。

183186 次浏览

cvWaitKey(0) stops your program until you press a button.

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.

Normal use

char k;


k=cvWaitKey(0);


if(k == 'ESC')

with k you can see what key was pressed.

/* 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); */

Plain simply, cvWaitKey() sleeps for X miliseconds, waiting for any key to be pressed.

int cvWaitKey(int X);

If a key is pressed, this function returns the ASCII code of key. Or returns -1 if no keys were pressed during that time.

cvWaitKey(x) / cv::waitKey(x) does two things:

  1. It waits for x milliseconds 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 x <= 0, it waits indefinitely for the key press.)
  2. It handles any windowing events, such as creating windows with cv::namedWindow(), or showing images with 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().

The cvWaitKey simply provides something of a delay. For example:

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.

cvWaitKey(milliseconds) simply wait for milliseconds provided as a parameter for a next key stroke of keyboard.

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.

In short cvWaitKey(milliseconds) wait either for key press or millisecond time provided.

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.

. argument of 0 is interpreted as infinite

. in order to drag the highGUI windows, you need to continually call the cv::waitKey() function. eg for static images:

cv::imshow("winname", img);

while(cv::waitKey(1) != 27); // 27 = ascii value of ESC

waits milliseconds to check if the key is pressed, if pressed in that interval return its ascii value, otherwise it still -1