1. 程式人生 > >在Visual Studio 2017 環境下配置 OpenCV

在Visual Studio 2017 環境下配置 OpenCV

老闆最近跟我提後面中心下面的 Computer / Machine Vision 會有一個關於 OpenCV 的 Lab session 和 Pratical 模組需要我來輔助做 TA,第一堂課主要是告訴大家怎麼在 VS2017 中配置 OpenCV。所以把自己準備的資料也就一道翻譯成中文發在這裡了。當然如果大家有什麼具體問題還請留言私戳~

1. 下載Visual Studio 2017. (我的是學校提供的免費版本,不過其實其他版本的VS也是一樣可行的。在國內的小夥伴下載VS2015也是一樣的。)

2. 從GitHub 下載最新的 OpenCV 庫檔案。在這篇文章裡,我使用的是 3.4.3 版本,雖然 4.0.0 版本已經出來了,不過考慮到對MSc學生主要是實用性和穩定性更重要,還是使用 OpenCV3 版本比較合適。下載完成後,解壓 “opencv-3.4.3-vc14_vc15.exe

” 檔案,並放在你覺得舒服的的地方。

歸於Github中的下載位置會如圖所示:

3. 設定環境變數:(因為我採用的是英文環境,這裡就不做翻譯了,稍有偏差反而引起不必要的麻煩會。)

3.1 This PC >> (Right Click) Properties >> Advanced system settings >> Environment Variables >> (In System variables part) Path >> (Double Click) >> New;

3.2 現在不要把 3.1 步裡的視窗,現在你需要開啟你存放 OpenCV 的檔案,其大體應該是如下這種路徑形式 “The Location wherever your opencv is …\opencv\build\x64\vc15\bin

” 。隨後,複製這個路徑回到 3.1 步中的視窗。然後就是一路點確認罷了。

4. 現在開啟你的 VS2017:

4.1 建立一個新的專案:File >> New >> Project;

4.2 選擇 Visual C++ 項 >> Empty Project >> Browse somewhere you would like to put this project. >> OK.

4.3 設定 debug 模式:

4.3.1 (右鍵你剛剛建立的專案) >> Properties >> set Configuration

item to “Debug” >>  set Platform item to “x64”;

4.3.2 Configuration Properties >> VC++ Directories >> Now, we need add three more path in the Include Directories item (as following shown).

隨後的步驟與之前類似,繼續到你存放 OpenCV 檔案的地方,然後找到如下路徑,複製它們到 Include Directories 中,並用 “;” 來分隔不同的路徑。

※ Your opencv file location …\opencv\build\include;

※Your opencv file location …\opencv\build\include\opencv;

※ Your opencv file location …\opencv\build\include\opencv2;

4.3.3 隨後是調整 Library Directories 項,前往如下路徑 “Your opencv file location …\opencv\build\x64\vc15\lib” ,將它複製到 Library Directories 項中。

4.3.4 現在前往左側欄的 Linker 項。 Linker >> Input >> Additional Dependencies >> add the “opencv_world343d.lib” item >> OK. (注意:檔名不一定要完全相同,主要取決於版本的不同。)

4.4 然後是設定 release 模式:這個跟 debug 步驟幾乎完全一樣,只是在最後一步,你需要複製的是 “opencv_world343.lib” 檔案到 Additional Dependencies 項中。

5. 測試:現在你可以右鍵 source file,然後新增一個 item。 隨後前往這個路徑 “Your opencv file location …\opencv\sources\samples\cpp” ,隨意複製任何一個 samples 程式碼到你的 main.cpp中,隨後測試其是否可以正常工作。