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顯示圖片時,窗口一閃而過,人眼無法看到。

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