What does OpenCV's cvWaitKey( ) function do?

2019-01-03 10:28发布

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.

标签: c++ c opencv
9条回答
聊天终结者
2楼-- · 2019-01-03 10:33

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.

查看更多
成全新的幸福
3楼-- · 2019-01-03 10:35

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.

查看更多
家丑人穷心不美
4楼-- · 2019-01-03 10:38
/* 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); */
查看更多
别忘想泡老子
5楼-- · 2019-01-03 10:42

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.

查看更多
【Aperson】
6楼-- · 2019-01-03 10:52

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

查看更多
smile是对你的礼貌
7楼-- · 2019-01-03 10:54

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 is zero, 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().

查看更多
登录 后发表回答