记:由opencv中的waitkey引发的一场血案

opencv中,waitkey这个函数相信大家都很熟悉了,其原型如下:

int waitKey(int delay=0)
Parameters: delay – Delay in milliseconds. 0 is the special value that means “forever”.

一直以来,我都是尊崇着opencv文档中推荐的代码去构建我的工程。最近我又构建了一个需要读入摄像头数据的工程,于是再次复制下一段标准代码来修改:

VideoCapture cap(0);  
for(;;){  
    Mat frame;  
    cap>>frame;  
    imshow("frame",frame);  
    if (waitKey(1) >= 0)
        break;
}  

有点空闲的时间,我就手欠,删掉了那个if语句快,我认为,这是在用来相应键盘事件的,而在我这个工程中,是不需要响应键盘事件的。在我理直气壮的删掉它以后,发现再也看不到摄像头的图像输出了,这是什么情况呢?百思不得其解,于是去翻看opencv文档,文档曰:

The function waitKey waits for a key event infinitely (when \texttt{delay}\leq 0 ) 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.

原来waitkey不仅仅是一个键盘事件等待函数,也是处理窗口事件的函数,真相大白!虽然不是很理解,opencv为什么要这么设计,还需要继续深入学习。

你可能感兴趣的:(C++)