標籤:
在同學的協助下,終於成功配置了vs+opencv,將詳細過程記錄在此,方便以後查閱
參考1
安裝vs2012
下載vs2012,官方
- 雙擊安裝檔案,設定安裝路徑
2.選擇需要的安裝包
3.安裝進行中
4.終於安裝完成
5.點擊啟動
提示輸入產品秘鑰
YKCW6-BPFPF-BT8C9-7DCTH-QXGWC
啟用成功
6.出現其它配置頁面
這裡選擇C++為預設開發環境
7.啟動
8.注意,有時可能出現相容性問題,需要下載更新包
點擊安裝即可
安裝opencv
1.下載opencv源碼
opencv官網地址:http://opencv.org/,網速有點小慢
點擊Download,在彈出的新頁面中選擇需要下載的opencv版本,這裡選擇2.4.9
然後,開始下載
大概349MB
2.解壓源碼
解壓路徑任意
解壓密碼
3.為opencv配置環境變數
(1)電腦右鍵“屬性”
(2)進階系統設定
(3)點擊環境變數
(4)在名稱為PATH的環境變數中添加opencv的bin檔案夾路徑,具體地:
①32位系統,添加:D:\File Program\opencv\build\x86\vc11\bin
②64位系統,添加:
D:\File Program\opencv\build\x86\vc11\bin (對應於32位系統)
D:\File Program\opencv\build\x64\vc11\bin (對應於64位系統)
目的是為了可以在編譯器Win32和X64中來回切換
注1:D:\File Program\opencv為opencv的解壓路徑
注2:vc11表示編譯環境為Microsoft Visual Studio 2012;
vs2012中配置opencv
1.開啟vs2012
2.建立win32控制台應用程式
3.在方案總管處:源檔案->添加->建立項
4.在屬性管理員處進行配置
(在屬性管理員中進行一次配置,就相當於進行了通用的配置過程,以後建立的工程就不用再額外的進行重新設定了)
(1)視圖->屬性管理員
(2)彈出了屬性管理員
(3)配置Debug Win32
註:這裡討論的是Debug,Release的配置完全類似
在Debug | Win32處的Microsoft.Cpp.Win32.user右鍵“屬性”
彈出Microsoft.Cpp.Win32.user屬性頁面
(4)開始配置Microsoft.Cpp.Win32.user屬性頁面
① 添加包含目錄,從而,C++程式可以include opencv
具體地:在彈出的屬性頁面中,找到”VC++目錄“,在對應的右側包含目錄中,輸入opencv的build->include目錄
可以看到,opencv->build->include目錄下有兩個檔案夾,應該將該大目錄和兩個檔案夾目錄都加入到include目錄下
即將如下三個目錄都添加到包含目錄中
D:\File Program\opencv\build\include
D:\File Program\opencv\build\include\opencv
D:\File Program\opencv\build\include\opencv2
②添加庫目錄
仍舊在剛剛的VC++目錄右側,在庫目錄中添加opencv->build->x86->vc11->lib路徑
即:D:\File Program\opencv\build\x86\vc11\lib
注意:這裡都是用x64還是x86取決與編譯器(而不是作業系統!)
如所示,編譯器為Win32,那麼,就應該選擇x86目錄下的庫
另外:
vc10 = Visual Studio 2010
vc11 = Visual Studio 2012
vc12 = Visual Studio 2013
③添加連結庫(即dll)
在“Microsoft.Cpp.Win32.user屬性頁面”的連結器->輸入的右側找到附加依賴項
添加如下dll名稱(2.4.9版本-Debug對應的opencv中的動態連結程式庫)
opencv_ml249d.lib
opencv_calib3d249d.lib
opencv_contrib249d.lib
opencv_core249d.lib
opencv_features2d249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
opencv_legacy249d.lib
opencv_objdetect249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_nonfree249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_videostab249d.lib
註:如果配置的是Release | Win32,那麼,需要添加的是如下19個不帶d的release版
opencv_objdetect249.lib
opencv_ts249.lib
opencv_video249.lib
opencv_nonfree249.lib
opencv_ocl249.lib
opencv_photo249.lib
opencv_stitching249.lib
opencv_superres249.lib
opencv_videostab249.lib
opencv_calib3d249.lib
opencv_contrib249.lib
opencv_core249.lib
opencv_features2d249.lib
opencv_flann249.lib
opencv_gpu249.lib
opencv_highgui249.lib
opencv_imgproc249.lib
opencv_legacy249.lib
opencv_ml249.lib
另外,對於opencv2.4.8
Debug版
opencv_ml248d.lib
opencv_calib3d248d.lib
opencv_contrib248d.lib
opencv_core248d.lib
opencv_features2d248d.lib
opencv_flann248d.lib
opencv_gpu248d.lib
opencv_highgui248d.lib
opencv_imgproc248d.lib
opencv_legacy248d.lib
opencv_objdetect248d.lib
opencv_ts248d.lib
opencv_video248d.lib
opencv_nonfree248d.lib
opencv_ocl248d.lib
opencv_photo248d.lib
opencv_stitching248d.lib
opencv_superres248d.lib
opencv_videostab248d.lib
Release版
opencv_objdetect248.lib
opencv_ts248.lib
opencv_video248.lib
opencv_nonfree248.lib
opencv_ocl248.lib
opencv_photo248.lib
opencv_stitching248.lib
opencv_superres248.lib
opencv_videostab248.lib
opencv_calib3d248.lib
opencv_contrib248.lib
opencv_core248.lib
opencv_features2d248.lib
opencv_flann248.lib
opencv_gpu248.lib
opencv_highgui248.lib
opencv_imgproc248.lib
opencv_legacy248.lib
opencv_ml248.lib
測試opencv是否配置成功
建立空白win32控制台應用程式
在項目中添加一個cpp,在cpp中輸入如下代碼
註:一定要保證項目路徑下由pic映像!
或者是直接給出映像的絕對路徑(這樣可以避免路徑錯誤)
#include<iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; int main() { // 讀入一張圖片(遊戲原畫) Mat img=imread("pic.jpg"); // 建立一個名為 "遊戲原畫"視窗 cvNamedWindow("遊戲原畫"); // 在視窗中顯示遊戲原畫 imshow("遊戲原畫",img); // 等待6000 ms後視窗自動關閉 waitKey(6000); }
(1)編譯
成功,沒有編譯錯誤
(2)運行程式,成功顯示映像
注1:如果路徑下沒有要操作的映像,會出現如下錯誤
安裝visual studio 2012,並配置opencv