What does OpenCV's cvWaitKey( ) function do?
cvWaitKey(x) / cv::waitKey(x)
does two things:
- 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 is 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()
.
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()
.
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(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.