OpenCV--入門

環境配置

1.操作系統:Win10 x64
2.Visual Studio : 2015
3.OpenCV : 3.2.0

下載

1.OpenCV-3.2.0程序;
2.官方文檔;

安裝與配置

說明:本文檔只講安裝官方預編譯好的程序,需自行編譯的請參考官方文檔;
1.下載後右鍵管理員權限運行安裝,一路下一步即可;
2.管理員權限運行cmd窗口,執行以下命令:

setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc14

3.編輯環境變量”PATH”,加入”%OPENCV_DIR%\bin”,重啓電腦(這一步我沒做,運行時報錯“程序無法啓動,系統找不到opencv_world320.dll”);
4.打開Visual Studio,新建一個空項目:

這裏寫圖片描述

5.項目屬性頁中設定好以下幾個選項(官方文檔有說明,圖片也大,具體內容不多說了):

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

6.新建cpp文件,將官方提供的代碼粘貼進去;

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
        return -1;
    }

    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file

    if (!image.data) // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }

    namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
    imshow("Display window", image); // Show our image inside it.

    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

7.如果是在Visual Studio中運行,則需配置參數:

這裏寫圖片描述

參數內容爲你想顯示的圖片的文件名,配置好後,將該圖片放置在cpp文件同級目錄下,按ctrl+F5運行即可看到結果。

8.如果是發佈成exe文件,可在cmd中引用並帶上圖片名稱作爲參數(注意路徑)即可,不寫了。

發佈了34 篇原創文章 · 獲贊 14 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章