openCV中waitKey函数介绍


#include < opencv2/highgui/highgui.hpp >

#include iostream >

#include < Windows.h >

using namespace cv;

using namespace std;

 

int main()

{

    Mat im;

    double duration;

    im = imread("D:\\Document\\pictures\\Lena.bmp",0);

 

    // 测试没有namedWindow时的waitKey执行时间

    duration = static_cast<</span>double>(getTickCount());

    waitKey(10000);

    duration = static_cast<</span>double>(getTickCount())

             - duration;

    duration /= getTickFrequency();

    cout <<"第一个waitKey运行时间为:" << duration

          << 's' << endl;

 

    namedWindow("Lena");

    imshow("Lena",im);

 

    // 测试有namedWindow时的waitKey执行时间

    duration = static_cast<</span>double>(getTickCount());

    waitKey(10000);

    duration = static_cast<</span>double>(getTickCount())

             - duration;

    duration /= getTickFrequency();

    cout <<"第二个waitKey运行时间为:" << duration

          << 's' << endl;

 

    Sleep(20000);

    return 0;

执行结果如下:

openCV中waitKey函数介绍

         从执行结果可以看出,第一个结果并未如期执行,第二个结果如期执行了。这是因为waitKey仅对窗口机制起作用,即namedWindow产生的窗口。若在此之前没有产生窗口,则waitKey相当于未执行。

 

waitKey有两个作用:

1. It waits for x milliseconds for a key press. If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns -1.

2It handles any windowing events, such as creating windows with cv::namedWindow(), or showing images with cv::imshow().

 

    格式:

waitKey(x);

第一个参数: 等待x ms,如果在此期间有按键按下,则立即结束并返回按下按键的

ASCII码,否则返回-1

如果x=0,那么无限等待下去,直到有按键按下

 

         另外,在imshow之后如果没有waitKey语句则不会正常显示图像。

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