opencv學習之等待按鍵事件-waitKey函數


文章來源:

https://mangoroom.cn/opencv/opencv-learning-waitKey.html

waitKey函數屬於opencv函數里既常用又非常基礎的函數,無論是剛開始學習opencv,還是使用opencv進行開發調試,都可以看到waitKey函數的身影。然而最基礎的東西可能往往容易忽略掉,在此可以好好了解一遍這個基礎又常用的waitKey函數。

waitKey函數原型

翻閱opencv的官方文檔,可以查閱到該函數的原型為


int cv::waitKey	(	int 	delay = 0	)	
Python:
retval	=	cv.waitKey(	[, delay]	)

從函數的原型可以了解到該函數以下幾點的信息

  • waitKey函數屬於cv命名空間
  • 一個int類型的參數,默認值為0,根據delay的名稱可以猜測參數值是一個延時值
  • 函數返回值為int類型

以上就是通過函數原型可以解讀到的信息了,關於waitKey函數的更多,往下繼續了解。

waitKey函數詳解

同樣地,繼續參考權威的opencv官方文檔,文檔對waitKey函數的解釋有

  • 1

Waits for a pressed key.

等待一個按鍵。 可以理解為此函數的功能是等待一個按鍵按下。

  • 2

The function waitKey waits for a key event infinitely (when delay≤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.

以上芒果理解為:
函數 Waitkey 在參數delay為正整數n時,延遲n毫秒,或者無限等待按鍵事件 (delay≤0時) 。由於操作系統在切換線程之間需要時間, 該函數不會等待完全延遲n ms, 它將等待至少延遲n ms, 這具體取決於當時計算機上運行的其他時間。如果在指定的時間之內沒有按下鍵, 則返回按下的鍵或-1 的ascii碼。函數的返回值是鍵盤按鍵鍵值的ascii碼。

  • 3

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.

此函數是 HighGUI 中唯一可以提取和處理事件的方法, 因此需要定期調用它進行正常的事件處理, 除非在處理事件處理的環境中使用 HighGUI。

  • 4

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.

該函數僅在至少創建了一個 HighGUI 窗口並且該窗口處於活動狀態時才有效。如果有多個 HighGUI 窗口, 則其中任何一個都可以處於活動狀態。

  • 5

delay Delay in milliseconds. 0 is the special value that means "forever".

延遲延遲以毫秒為單位。0是表示 "永遠" 的特殊值。即參數值為0時,waitKey函數等待的時間是無限長。


通過以上對官方文檔的學習,對waitKey的認識可以歸為:waitKey函數是一個等待鍵盤事件的函數,參數值delay<=0時等待時間無限長,delay為正整數n時至少等待n毫秒的時間才結束。在等待的期間按下任意按鍵時函數結束,返回按鍵的鍵值(ascii碼),等待時間結束仍未按下按鍵則返回-1。該函數用在處理HighGUI窗口程序,最常見的便是與顯示圖像窗口imshow函數搭配使用

waitKey函數用法

waitKey函數非常基礎以及常用,以下是常見的一些用法

  • 1
cv::imshow("windowname", image);
cv::waitKey(0);//任意按鍵按下,圖片顯示結束,返回按鍵鍵值
  • 2
cv::imshow("windowname", image);
cv::waitKey(10);//等待至少10ms圖片顯示才結束,期間按下任意鍵圖片顯示結束,返回按鍵鍵值
  • 3
 VideoCapture cap("video.mp4"); 
    if(!cap.isOpened()) 
    { 
        return -1; 
    } 
    Mat frame;  
    while(true) 
    { 
        cap>>frame; 
        if(frame.empty()) break;
        imshow("windowname",frame); 
        if(waitKey(30) >=0) //延時30ms,以正常的速率播放視頻,播放期間按下任意按鍵則退出視頻播放,並返回鍵值
            break;
    } 
  • 4
 VideoCapture cap("video.mp4"); 
    if(!cap.isOpened()) 
    { 
        return -1; 
    } 
    Mat frame;  
    while(true) 
    { 
        cap>>frame; 
        if(frame.empty()) break;
        imshow("windowname",frame); 
        if(waitKey(30) == 27) //延時30ms,以正常的速率播放視頻,播放期間按下esc按鍵則退出視頻播放,並返回鍵值
            break;
    } 

尾巴

waitKey函數是非常簡單而且常見的,開始入門的時候需要掌握好它,開發調試的時候waitKey函數同樣是一個好幫手。


本文由芒果浩明發布,轉載需注明來源。
本文鏈接:https://mangoroom.cn/opencv/opencv-learning-waitKey.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM