記:opencvのwaitkeyによる血液事件

1935 ワード

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がなぜこのように設計されているのか、さらに深く学ぶ必要があります.