天天看點

imread(),imshow(),nameWindow(),waitKey()的基本用法imread(),imshow(),nameWindow(),waitKey()的基本用法

imread(),imshow(),nameWindow(),waitKey()的基本用法

imread(const string& filename, int flags = 1)

imread()為讀取圖檔函數。

第一個參數:為圖檔檔案路徑

第二個參數為加載類型。第二個參數,可控制不同加載方式。第二個參數預設是1,其含義為按照RGB格式加載圖檔。一般而言,預設加載即可。

以下是opencv定義第二個參數數值的含義。

IMREAD_UNCHANGED = -, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE = ,  //!< If set, always convert image to the single channel grayscale image.
 IMREAD_COLOR= ,  //!< If set, always convert image to the  channel BGR color image.
IMREAD_ANYDEPTH= ,  //!< If set, return -bit/-bit image when the input has the corresponding depth, otherwise convert it to -bit.
IMREAD_ANYCOLOR= ,  //!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL= ,  //!< If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2  = , //!< If set, always convert image to the single channel grayscale image and the image size reduced /
IMREAD_REDUCED_COLOR_2= , //!< If set, always convert image to the  channel BGR color image and the image size reduced /
IMREAD_REDUCED_GRAYSCALE_4  = , //!< If set, always convert image to the single channel grayscale image and the image size reduced /
IMREAD_REDUCED_COLOR_4= , //!< If set, always convert image to the  channel BGR color image and the image size reduced /
IMREAD_REDUCED_GRAYSCALE_8  = , //!< If set, always convert image to the single channel grayscale image and the image size reduced /
IMREAD_REDUCED_COLOR_8      =   //!< If set, always convert image to the  channel BGR color image and the image size reduced /
           

在絕大部分情況,我們使用imread()時隻需填寫第一個參數,讓opencv預設按照RGB格式加載圖檔,因為opencv很多庫函數都是基于RGB存儲空間對圖檔進行處理的。

nameWindow(const string& windowName, int flags = 1)

nameWindow()是建立顯示視窗函數,該函數可實作自動建立和釋放句柄,使用完成無需人工釋放。

第一個參數:建立視窗的名稱

第二個參數:視窗的顯示方式。常見的方式有全屏顯示、圖檔大小顯示和自适應大小顯示等。預設情況下是CV_WINDOW_AUTOSIZE,即flags=1。

CV_WINDOW_AUTOSIZE    自适應大小顯示
CV_WINDOW_FULLSCREEN  全屏顯示
CV_WINDOW_NORMAL      通常顯示

至于其他的,可自行在opencv函數定義裡尋找
           

imshow(const string& winname, InputArray mat);

該函數為在指定的視窗内顯示制定的圖檔。

第一個參數:為視窗名稱,視窗名稱須在nameWindow建立過。

第二個參數:為圖檔InputArray 類型的Mat。

waitKey(int delay = 0)

這個函數是在一個給定的時間内(機關ms)等待使用者按鍵觸發;如果使用者沒有按下 鍵,則接續等待(循環)

delay參數為延時時間,機關是ms。waitKey()函數必須要調用,否則圖檔不會顯示。

waitKey();   //代表不會自動關閉視窗,直到鍵盤按鍵按下才退出。
waitKey();  //代表50ms後自動關閉視窗,或者按鍵按下關閉視窗
           

代碼測試【預設加載方式】

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
int main(int argc, char** argv) {
    Mat src = imread("G:\\2.jpg");
    namedWindow("test", CV_WINDOW_NORMAL);
    imshow("test", src);
    waitKey();
    return ;
}
           

運作效果:

imread(),imshow(),nameWindow(),waitKey()的基本用法imread(),imshow(),nameWindow(),waitKey()的基本用法

代碼測試【imread()裡灰階圖加載】

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
int main(int argc, char** argv) {
    Mat src = imread("G:\\2.jpg",IMREAD_GRAYSCALE);
    namedWindow("test", CV_WINDOW_NORMAL);
    imshow("test", src);
    waitKey();
    return ; 
}
           

運作效果:

imread(),imshow(),nameWindow(),waitKey()的基本用法imread(),imshow(),nameWindow(),waitKey()的基本用法

總結

在opencv中顯示圖檔,一般需要使用到三個函數。

1.imread()

2.nameWindow()

3.imshow()

4.waitKey()

即先加載圖檔,再建立顯示視窗,然後制定視窗顯示圖檔,最後調用

最後,祝大家實驗成功。