OpenCV C++ 01 - Load Image from File and Display
前言
雖然之前已經(jīng)在一定程度上接觸、學習并應(yīng)用了 OpenCV,但是依然想系統(tǒng)地對這些內(nèi)容重新學習和整理。
Code
/* 作者:鄭大峰 時間:2019年09月19日 環(huán)境:OpenCV 4.1.1 + VS2017 內(nèi)容:Load Image from File and Display */#include "pch.h" // 這個是預編譯頭文件,VS2017之前的版本為stdafx.h,詳細請自行搜索 #include <iostream> #include <opencv2/opencv.hpp> // 包含OpenCV庫的頭文件using namespace std; using namespace cv; // OpenCV中所有的類、函數(shù)和數(shù)據(jù)結(jié)構(gòu)都在該命名空間下定義int main() {// 相對路徑注意事項// 1)圖片放在代碼文件同級目錄下,啟動調(diào)試可正常讀取圖像。但是雙擊生成的可執(zhí)行文件,則讀取不到圖像。// 2)復制圖像至生成的可執(zhí)行文件同級目錄下,雙擊可執(zhí)行文件,可正常讀取圖像string image_file = "claudia.png";// 如果同時添加Debug或者Release對應(yīng)的依賴,這里會讀取不到圖像Mat img = imread(image_file, IMREAD_UNCHANGED);// If imread() function fails to load the image, the returned Mat object will be empty. // If the Mat object is empty, image.empty() function will return true.if (img.empty()){cout << "Image is empty!";return -1;}namedWindow(image_file, WINDOW_AUTOSIZE); // Create a windowimshow(image_file, img); // Show our image inside the created window.waitKey(0); // 一直等待按鍵輸入。當按鍵被點擊時,返回點擊按鍵的ASCII碼return 0; }Result
Explanation
Mat imread(const String& filename, int flags = IMREAD_COLOR)
This function loads an image from the specified file and return is as
a Mat object. If the function cannot read the file, it will return an
empty Mat object.
relative to your cpp file. jpeg, jpg, bmp, png, tiff and tif image
file types are always supported. Other image file types are supported
depending on your platform and installed codecs.
argument such that default IMREAD_COLOR argument will be used.
- IMREAD_UNCHANGED - The image will be loaded as it is. If you want to get the alpha channel in your input image (if it is available), you
have to use this flag. - IMREAD_GRAYSCALE - The image will be load as a gray-scale image (i.e. - Single channel image, Black and white image)
- IMREAD_COLOR - The image will be loaded as a BGR image (i.e. - 3 channel image, color image)
void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE)
This function creates a window which can be used to place images and track bars. If a window already exists with the given name, this function does nothing.
- WINDOW_AUTOSIZE - User cannot resize the window. Image will be displayed in its original size.
- WINDOW_NORMAL- User can resize the window.
void imshow(const String& winname, InputArray mat)
This function shows the image in a window specified by winname. If the
window is created with WINDOW_AUTOSIZE flag, image will be displayed
in its original size. Otherwise image may be scaled to the size of the
window. If the window has not been created by calling to namedWindow()
function, this function will create a window with the WINDOW_AUTOSIZE
flag. This function call should be followed by waitKey(int)
function call in order to provide sufficient time to paint and display
the image in the window for the specified time duration in
milliseconds. If you do not call waitKey(int) function, the image will
not be displayed in the window.
轉(zhuǎn)載于:https://www.cnblogs.com/zdfffg/p/11551264.html
總結(jié)
以上是生活随笔為你收集整理的OpenCV C++ 01 - Load Image from File and Display的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django model、view拆分,
- 下一篇: OpenCV C++ 02 - Cre