32

Possible Duplicate:
OpenCV - cvWaitKey( )

I want to filter the video frame.

for(;;)
{
cap.read( frame);
medianBlur(frame,framedst,5);
imshow("frame",frame);
imshow("framedst",framedst);    
if( waitKey (30) >= 0) break;
}

What does the waitKey(30) mean? Because if I comment out the line if( waitKey (30) >= 0) break;, the above code doesn't work!

Community
  • 1
  • 1
Imbarfar
  • 351
  • 1
  • 3
  • 10
  • 2
    [This question](http://stackoverflow.com/questions/5217519/opencv-cvwaitkey) asks about `waitkey`, and I believe the answer explains why your `imshow` doesn't work. Quoting the answer, *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().* – chris Sep 17 '12 at 01:25
  • That's right ,I don't use OPENCV much – Imbarfar Sep 17 '12 at 02:06
  • @chris when I use a webcam as input, I do not have to call waitKey. But surely the system could be confused a bit about displaying too much in too little time. – Barney Szabolcs Oct 29 '12 at 21:21

1 Answers1

26

The function waitKey() waits for a key event for a "delay" (here, 30 milliseconds). As explained in the OpenCV documentation, HighGui (imshow() is a function of HighGui) need a call of waitKey regularly, in order to process its event loop.

Ie, if you don't call waitKey, HighGui cannot process windows events like redraw, resizing, input event, etc. So just call it, even with a 1ms delay :)

dolgom
  • 611
  • 1
  • 11
  • 27
tito
  • 12,990
  • 1
  • 55
  • 75
  • then that mean cWaitkey () does not have any relation to capture image after 30 milliseconds. It has to wait and display image on display window. – Abc Jan 06 '17 at 08:19
  • With `waitKey` I find the assumption that it will return -1 after timeout on Windows is sometimes wrong - instead it returns 255. and so many common codes exit their main loop almost immediately. It seems the solution to this cross-platform issue is to use `waitKeyEx` instead. – user2023370 Mar 29 '18 at 14:52