日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

OpenCV C++ 01 - Load Image from File and Display

發布時間:2024/7/5 c/c++ 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV C++ 01 - Load Image from File and Display 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

雖然之前已經在一定程度上接觸、學習并應用了 OpenCV,但是依然想系統地對這些內容重新學習和整理。

Code

/* 作者:鄭大峰 時間:2019年09月19日 環境:OpenCV 4.1.1 + VS2017 內容: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中所有的類、函數和數據結構都在該命名空間下定義int main() {// 相對路徑注意事項// 1)圖片放在代碼文件同級目錄下,啟動調試可正常讀取圖像。但是雙擊生成的可執行文件,則讀取不到圖像。// 2)復制圖像至生成的可執行文件同級目錄下,雙擊可執行文件,可正常讀取圖像string image_file = "claudia.png";// 如果同時添加Debug或者Release對應的依賴,這里會讀取不到圖像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.

  • filename - You have to give the relative or absolute path of an image file. If you are giving the relative path, it should be
    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.
  • flags - There are several possible values for the flag argument. In the above program, I did not pass any value to this
    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.

  • winname - Name of the window. That name will display in the title bar of the newly created window. This name is also the identifier for this window and it will be used in the later OpenCV function calls to identify the window.
  • flags - Determine the size of the window. In the above program, I did not pass any value to this argument such that default WINDOW_AUTOSIZE argument will be used.
    • 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.

  • winname - Name of the window which created by namedWindow() function.
  • mat - Mat object which holds the image
  • 轉載于:https://www.cnblogs.com/zdfffg/p/11551264.html

    總結

    以上是生活随笔為你收集整理的OpenCV C++ 01 - Load Image from File and Display的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。