[圖文] OpenCV 4.0.1 安裝配置在 Visual Studio 2019

2024-02-04
1. 新版的 Visual Studio 2022 + OpenCV490 可以參考新文章https://charlottehong.blogspot.com/2024/02/opencv-490-win11-visual-studio-2022.html
軟體下載
先到官方下載這兩項軟體 (點擊有連結)
- OpenCV401:https://sourceforge.net/projects/opencvlibrary/files/4.0.1/opencv-4.0.1-vc14_vc15.exe/download
(我是故意放帶版本號連結的,這樣下面教學才能直接貼上不用改版本號。)
本文範例opencv載官方的這個

並直接安裝到C曹
這邊建議跟著我做,因為路徑如果改了,本篇教程很多部分也要跟著改
一般都是看著教程複製貼上這做,到時候很容易漏掉沒改到…

- Visual Studio 2019
關於 Visual Studio 如何安裝可以參考
https://charlottehong.blogspot.com/2018/09/c-visual-studio-2017.html
文件說明
build 是待會要用到的文件,全部都在裡面

這個位置是要設置在環境變數的(專給VS用的)
C:\opencv\build\x64\vc15\bin
這個位置給 C++ 引入標頭檔的
C:\opencv\build\include
這個位置專給 VS 引入lib檔的
C:\opencv\build\x64\vc15\lib
以上如果是 VS2015 就是找vc14
部屬OpenCV
環境變數
這是一次性的工作,設置好環境變數,這台電腦以後就不用再設置了。
對著左下角的開始按右鍵,打開管理員模式的 powershell。

如果是使用 win7 的用戶
- 用管理員打開cmd,然後在cmd內輸入 powershell 就可打開
(非SP1版本可能沒有…參考文中置頂的舊文設置)
輸入下面指令 (連點三下直接全選)
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\opencv\build\x64\vc15\bin", "Machine")

輸入之後不會有什麼提示,想要確定是否成功可以輸入
[environment]::GetEnvironmentVariable("PATH", "Machine")
查詢目前環境變數是否有輸入

檢查的話複製紅框部分,然後打開資料夾貼上路徑欄並打開,檢查看看是不是真的有東西。
- 噴這個錯誤
LNK1104 無法開啟檔案 'opencv_world401d.lib'
,也是回來檢查紅框路徑。 - 記得VS在此之前不要打開歐,如果已經先打開了,這邊做完後關閉重新啟動VS就好了。
- 終端機要用管理權限開啟,不然那行指令會沒作用。
VS2019設定
讓我們先建立新專案吧

直接選擇空白專案

這邊隨意打一個名稱就可以下一步了

創建好專案再來就設置opencv吧~先新增屬性表
這張圖多看幾眼,右鍵的位置點錯就錯了…

然後新增名稱 opencv401d.props
這邊不要自己改,照著打稍後會說明

好了之後會出現在這個位置,點他兩下

加入 include 路徑 和 lib 路徑
include目錄的最前面加上 C:\opencv\build\include;
函式庫目錄最前面加上 C:\opencv\build\x64\vc15\lib;
(他原本是有東西的,原本的東西要留著中間用分號隔開)

加入 lib 檔案
這邊點那個小三角,在按編輯

然後輸入 opencv_world401d.lib

這邊補充說明一下 opencv_world401d.lib 中的 401 是 opencv 的版本 4.0.1版本,如果下載的是別的版本,記得要自己修正。
測試代碼
現在讓我們新增一份新的程式碼

直接就預設名稱下一步吧

接下來輸入測試程序
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char const* argv[]) {
Mat img(270, 720, CV_8UC3, Scalar(56, 50, 38));
line(img, Point(20, 40), Point(120, 140), Scalar(255, 0, 0), 3);
rectangle(img, Point(150, 40), Point(250, 140), Scalar(0, 0, 255), -1);
circle(img, Point(330, 90), 50, Scalar(0, 255, 0), -1);
ellipse(img, Point(460, 90), Size(60, 40), 45, 0, 360, Scalar(255, 255, 0), 2);
Point points[1][5];
int x = 40, y = 540;
points[0][0] = Point(0 + y, 50 + x);
points[0][1] = Point(40 + y, 0 + x);
points[0][2] = Point(110 + y, 35 + x);
points[0][3] = Point(74 + y, 76 + x);
points[0][4] = Point(28 + y, 96 + x);
const Point * ppt[1] = { points[0] };
int npt[] = { 5 };
polylines(img, ppt, npt, 1, 1, Scalar(0, 255, 255), 3);
putText(img, "Test Passed !!", Point(10, 230), 0, 3, Scalar(255, 170, 130), 3);
namedWindow("OpenCV Test By:Charlotte.HonG", WINDOW_AUTOSIZE);
imshow("OpenCV Test By:Charlotte.HonG", img);
waitKey(0);
return 0;
}
像這個樣子

記得確認一下上方是不是 debug/x64 模式,因為剛剛只有設置這個模式。

然後就按F5測試吧

恭喜完成 opencv 的設置~到這邊就已經設置完畢。
練習
趁著現在記憶猶新,趕快來練習,現在就試著設置 relese 模式吧~
目前是設置 debug/x64 模式,如果要設置 release 模式,最初一開始的時候屬性表要加在 release | x64 裡面。

而在 release 最後面加入lib的時候要使用沒有 d 結尾的 `opencv_world401.lib,才是正確的檔案,這邊設置錯誤會無法編譯。
設置完畢代碼不需要更動,編譯的時候VS上方從 debug/x64 模式更改為 release/x64 模式即可~
至於 x86 模式因為 官方給的檔案並沒有幫你預先編譯好,就沒辦法做了,想要使用x86模式需要自己重新編一,詳細可以參考這邊站內文章重新編譯。
https://charlottehong.blogspot.com/2017/07/opencv-320-contrib-visual-studio.html
重複使用
現在已經設置完畢了,並且你會發現,在你的專案目錄會有一個 opencv401d.props
剛剛要你不要更改就是因為連同版本號一起打上來,這樣比較好識別。

這個把他複製出來存好,他是可以重複使用的
下次開別的新專案直接新增進來就好,完全不用再設置opencv。
使用方式像這樣,加入這個屬性表

需要注意的是屬性表建議是複製一份到專案內,然後才按上圖紅框的加入現有屬性表!而不是打開A專案,然後瀏覽到B專案的位置,加入B專案的屬性表,
這是為了確保A專案的獨立性,不這樣做哪天B專案砍掉了,或是B專案有變動到時候A會跟著壞掉…
最後
有什麼問題可以在下方留言發問喔。
最後如果還是不行的話可以參考我幫你設置好的專案,載下來打開就可以用瞜。
(裡面有設置好的 debug/rekease 屬性表,你也可以載下來直接複製過去用。)
https://github.com/hunandy14/CHG_OpenCV_test/tree/OpenCV-4.0.1
覺得本篇教學章寫的很清楚,很有幫助的話,不妨留個言讓我知道吧~
最重要的是,分享給你的朋友們,讓他們一起來學習opencv吧~
短網址幫你設置好了
http://bit.ly/2U0DEiQ [圖文] OpenCV 4.0.1 安裝配置在 Visual Studio 2019
其他關連文章
最新 OpenCV 4.5.1 文章可以參考這邊,用另一種方式配置只要搞一次就好,往後開新專案都是直接用,完全不需要設置(有懶人包一鍵配置)
https://charlottehong.blogspot.com/2021/01/opencv-451-visual-studio-2019.html
舊版的 Visual Studio 2017 可以參考這篇https://charlottehong.blogspot.com/2017/06/opencv-320-vc14-visual-studio-2017.html