103

What happens during the execution of cvWaitKey()? What are some typical use cases? I saw it in OpenCV reference but the documentation isn't clear on its exact purpose.

StockB
  • 755
  • 1
  • 13
  • 33
Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 3
    What don't you understand about it or what do you think it does? The OpenCV documentation reference seems very clear to me... – Daniel Sloof Mar 07 '11 at 08:44
  • 9
    @user588855 It's important to all of us that you review your questions and accept some answers, including this. – karlphillip Mar 31 '11 at 22:38
  • 4
    For the record, `cvWaitKey()` is now a deprecated C function. `waitKey()` is the modern C++ equivalent. – StockB Jan 24 '15 at 19:31
  • 1
    Can you please mark one of the answers (@SuperElectric 's seems the best for now) so that the question can be considered answered. 4 years of waiting for an answer and getting multiple answers is a long period of time... – rbaleksandar Aug 14 '15 at 12:52
  • @DanielSloof for instance, it would be nice to know which standard do the return values follow. – lahjaton_j Sep 02 '15 at 10:21

9 Answers9

156

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().

SuperElectric
  • 17,548
  • 10
  • 52
  • 69
  • 3
    For the future readers: Note that `cv2.waitKey(x)` or `cv::waitKey(x)` waits for a key event infinitely not only if *`x = 0`* but also if *`x < 0`* i.e. **`x <= 0`**. Source: https://docs.opencv.org/4.x/d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7 – Milan Mar 24 '22 at 16:07
24

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.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 3
    That's not quite the whole story. In practice, even if you don't care about keyboard input, calling this function can be essential to some OpenCV applications. There's some [more information here](http://opencv.willowgarage.com/wiki/documentation/c/highgui/WaitKey). – Drew Noakes Jan 27 '13 at 23:15
  • @DrewNoakes the link you have shared is broken. If possible, could you please take a look and update it? Thank you! – Milan Jun 28 '21 at 22:14
  • 1
    Here is the URL via the wayback machine: https://web.archive.org/web/20120122022754/http://opencv.willowgarage.com/wiki/documentation/c/highgui/WaitKey – Drew Noakes Jun 29 '21 at 05:07
14

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.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
Jorge Vega Sánchez
  • 7,430
  • 14
  • 55
  • 77
  • 6
    Also slightly more importantly - it's the openCV message loop. You won't see any new data in a cvNamedWindow unless you call cvWaitKey() in the loop – Martin Beckett Mar 07 '11 at 15:05
  • 16
    -1 from me for doing a comparison **char ?= multichar**. This is just wrong. Use either the printable character (which in this case is not possible since ESC is a non-printable character but in case for example of using the Q key you can do a ?= 'q' comparison) or pick one of the numerical representations. The ASCII supports multiple numerical systems: hexadecimal (0x1b), decimal (27) and octal (033). All listed represent the ESC key. – rbaleksandar Aug 14 '15 at 12:46
  • 1
    should be int not char, then use bit ops – Vlad Oct 22 '18 at 21:56
  • @Vlad Yes, it should be 27 as in user1564486 – FindOutIslamNow Nov 06 '18 at 14:55
9

. 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

8

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.

ForceMagic
  • 6,230
  • 12
  • 66
  • 88
Andy
  • 81
  • 1
  • 1
  • Welcome on SO, it is a good place to post information like you did. Usually, when you post an answer it must bring new informations to the question, so it doesn't creates redundant post. Which was the case for your post. If you have smaller comment, you could also comment on the question later on (when you get more Rep). If you haven't read it yet, I suggest you to have a look to the FAQ : http://stackoverflow.com/faq. Regards – ForceMagic Oct 11 '12 at 06:02
5
/* 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); */
karlphillip
  • 92,053
  • 36
  • 243
  • 426
enthusiasticgeek
  • 2,640
  • 46
  • 53
5

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.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
sue-ling
  • 399
  • 2
  • 11
  • 21
1

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.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
Pravin Dahale
  • 91
  • 1
  • 9
  • 1
    "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" does this really matter to the question? – quantum Oct 20 '12 at 01:40
0

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