opencv imshow显示图像时,为啥后面要调用 waitKey(0)

#include <opencv2/opencv.hpp>
using namespace cv;

void main()
{
	Mat srcImage = imread("1.jpg");
	imshow("original picture", srcImage);
	waitKey(0);
}

waitKey(0);这行代码的功能是:让图片窗口一直显示,直到有按键按下。

该函数的说明如下:
/** @brief Waits for a pressed key.

The function waitKey waits for a key event infinitely (when \fKaTeX parse error: Undefined control sequence: \f at position 21: …tt{delay}\leq 0\̲f̲ ) or for delay
milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the
function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is
running on your computer at that time. It returns the code of the pressed key or -1 if no key was
pressed before the specified time had elapsed.

@note

This function is the only method in HighGUI that can fetch and handle events, so it needs to be
called periodically for normal event processing unless HighGUI is used within an environment that
takes care of event processing.

@note

The function only works if there is at least one HighGUI window created and the window is active.
If there are several HighGUI windows, any of them can be active.

@param delay Delay in milliseconds. 0 is the special value that means “forever”.
*/
CV_EXPORTS_W int waitKey(int delay = 0);
delay表示等待的时长,单位是毫秒
如果要等待5秒种,则可以让delay的值为5000。5秒钟之后,如果还没有按键被按下,则窗口直接退出。
delay值为0时,表示一直等待,直到有按键被按下。

如果没有调用这个函数,则调用imshow显示图片时,窗口一闪而过,人眼无法看到。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章