1、安裝visual studio2017
這個不用多說,相信大家都會。
2、下載OpenCV
https://github.com/opencv/opencv/releases/download/3.4.0/opencv-3.4.0-vc14_vc15.exe
從上面的網址下載並安裝(其實就是解壓縮),我的路徑是C:\opencv,安裝完畢后該目錄下還有build和sources兩個文件夾。其中的內容如下:
build:頭文件以及編譯好的庫文件,包括lib和dll。
sources:OpenCV的源代碼,可以自己重新進行編譯。
3、配置visual studio2017
3.1 新建工程
按照下圖所示,建立一個 windows console application
3.2 添加 include路徑、lib路徑、lib文件
打開新建項目的屬性頁,如下圖所示
3.2.1 添加include路徑
選中“Include Directories”屬性,添加: C:\opencv\build\include;
3.2.2 添加lib路徑
選中“Library Directories”屬性,添加: C:\opencv\build\x64\vc15\lib;
3.2.3 添加lib
選中“input”屬性,添加: opencv_world340d.lib;
4、測試
4.1 輸入代碼如下:
該代碼源自OpenCV Samples
1 // OpenCVPrj01.cpp : Defines the entry point for the console application. 2 // 3 4 #include "stdafx.h" 5 6 #include "opencv2/imgcodecs.hpp" 7 #include "opencv2/highgui.hpp" 8 #include <iostream> 9 10 using namespace cv; 11 using namespace std; 12 13 /** 14 * @function main 15 * @brief Main function 16 */ 17 int main(void) 18 { 19 double alpha = 0.5; double beta; double input; 20 21 Mat src1, src2, dst; 22 23 /// Ask the user enter alpha 24 cout << " Simple Linear Blender " << endl; 25 cout << "-----------------------" << endl; 26 cout << "* Enter alpha [0.0-1.0]: "; 27 cin >> input; 28 29 // We use the alpha provided by the user if it is between 0 and 1 30 if (input >= 0 && input <= 1) 31 { 32 alpha = input; 33 } 34 35 //![load] 36 /// Read images ( both have to be of the same size and type ) 37 src1 = imread("LinuxLogo.jpg"); 38 src2 = imread("WindowsLogo.jpg"); 39 //![load] 40 41 if (src1.empty()) { cout << "Error loading src1" << endl; return -1; } 42 if (src2.empty()) { cout << "Error loading src2" << endl; return -1; } 43 44 //![blend_images] 45 beta = (1.0 - alpha); 46 addWeighted(src1, alpha, src2, beta, 0.0, dst); 47 //![blend_images] 48 49 //![display] 50 imshow("Linear Blend", dst); 51 waitKey(0); 52 //![display] 53 54 return 0; 55 }
4.2 拷貝2張圖片(LinuxLogo.jpg和WindowsLogo.jpg)到源文件目錄下。
4.3 工具欄中的“solution platforms”一定要選擇“x64”
因為我們以上的設置都是基於x64平台的。opencv-3.4.0-vc14_vc15.exe 這個文件中只有x64的庫,如果32位平台的需要自行編譯,我會在另一篇文章《如何編譯OpenCV3.4.0》中說明如何自己編譯OpenCV。
4.4 運行
4.4.1 按F5或ctrl+F5運行程序,出現如下界面:
4.4.2 輸入0-1的數值:
4.4.3 顯示圖片疊加結果
運行到這一步,表示你已經能夠成功的在visual studio 2017 環境中使用OpenCV 3.4.0了。
2018-02-09 18:13:34