win10系統下vs2015+opencv3.4.3+cmake編譯opencv-contrib3.4.3
最近在研究KAZE演算法,發現我使用的opencv2.4.8中沒有整合KAZE演算法,網上查詢該演算法在opencv3版本中有,故將opencv和vs解除安裝重灌,我安裝的是opencv3.4.3+vs2015,一切配置完之後,發現KAZE演算法是可以用了,但SIFT/SURF演算法又出錯,原來是由於版權問題,將其放在了opencv_contrib擴充套件模組中,又重新編譯配置了一遍,該過程出現了多問題,特此將其寫下來,希望能幫上忙。
一、軟體下載
1、opencv下載
opencv下載大家應該都知道在官網下載,在此給出官網連結。https://opencv.org/releases.html
2、vs下載
同樣也給出官方連結。
https://visualstudio.microsoft.com/zh-hans/downloads/
3、cmake下載
cmake的版本與編譯成功與否幾乎沒有關係。下載網址為:
https://cmake.org/download/
4、opencv_contrib擴充套件模組的下載
在下載opencv_contrib擴充套件模組時,版本必須要和opencv的版本一致,否則將導致後面編譯錯誤(我第一次下載的版本與opencv不一致,出現各種錯誤,千萬不要入坑!!!)。
opencv_contrib擴充套件模組的下載網址為:
https://github.com/opencv/opencv_contrib/releases
我下載的是3.4.3版本,可以根據自己所需要的版本來下載。
二、軟體安裝
以上四個軟體的安裝很容易,按提示步驟安裝即可。
三、opencv_contrib擴充套件模組的編譯
這一部分是關鍵步驟,它成功了就算是整個編譯配置過程成功了一半。
1、首先找到cmake根目錄下bin資料夾,開啟cmake-gui.exe,介面如下圖所示。其中where is the source code 選擇你opencv的安裝路徑,where to build the binaries 選擇需要存放生成檔案的目錄,在你安裝的opencv根目錄下新建一個資料夾,建議用英文名命名,我將其命為build。
2、點選configure,第一次使用會彈出對話方塊,需選擇對應的vs版本,我電腦是64位的,故選擇的visual studio 14 2015 win64。
3、找到OPENCV_EXTRA_MODULES_PATH,將其值選擇為opencv_contrib資料夾中的modules資料夾,勾選對應的資料夾。同時要記得勾選OPENCV_ENABLE_NONFREE,否則測試時會出錯。
4.再次點選configure,完成之後點選generate,在cmake介面直接點選open project開啟專案,選擇64或32位的Debuge和Release,根據自己電腦系統來選擇,在選單欄選擇生成->生成解決方案,這一過程時間會比較長,耐心等待哦。
5.編譯完成後,會在opencv根目錄下的modules資料夾下生成如下子資料夾,將各個子資料夾中的include資料夾剪切合並,並且放到opencv根目錄下的include資料夾中。
四、opencv的配置
1.配置環境變數
右擊此電腦—>屬性—>高階系統設定—>環境變數—>選擇使用者變數中的PATH變數—>編輯,新增環境變數。我的環境變數如下圖所示(你們的按照自己的路徑來,其中build就是上面編譯opencv_contrib擴充套件模組時新建的資料夾):
2.opencv的配置
開啟vs2015,新建一個工程,選擇屬性管理器,點選工程名,根據自己電腦的系統進行配置。
雙擊上圖中紅色框圈出的,選擇VC++目錄,分別配置包含目錄和庫目錄,我的配置如下:
包含目錄:
D:\opencv-3.4.3\include
庫目錄:
D:\opencv-3.4.3\build\lib\Debug
D:\opencv-3.4.3\build\lib\Release
選擇連結庫—>輸入,新增依賴項。此處要根據opencv的版本號來配置,“343”代表我的opencv版本為3.4.3,其中帶d的為Debug模式,不帶d的為Release模式。
opencv_aruco343d.lib
opencv_bgsegm343d.lib
opencv_bioinspired343d.lib
opencv_calib3d343d.lib
opencv_ccalib343d.lib
opencv_core343d.lib
opencv_datasets343d.lib
opencv_dnn343d.lib
opencv_dnn_objdetect343d.lib
opencv_dpm343d.lib
opencv_face343d.lib
opencv_features2d343d.lib
opencv_flann343d.lib
opencv_fuzzy343d.lib
opencv_hfs343d.lib
opencv_highgui343d.lib
opencv_imgcodecs343d.lib
opencv_imgproc343d.lib
opencv_img_hash343d.lib
opencv_line_descriptor343d.lib
opencv_ml343d.lib
opencv_objdetect343d.lib
opencv_optflow343d.lib
opencv_phase_unwrapping343d.lib
opencv_photo343d.lib
opencv_plot343d.lib
opencv_reg343d.lib
opencv_rgbd343d.lib
opencv_saliency343d.lib
opencv_shape343d.lib
opencv_stereo343d.lib
opencv_stitching343d.lib
opencv_structured_light343d.lib
opencv_superres343d.lib
opencv_surface_matching343d.lib
opencv_text343d.lib
opencv_tracking343d.lib
opencv_ts343d.lib
opencv_video343d.lib
opencv_videoio343d.lib
opencv_videostab343d.lib
opencv_xfeatures2d343d.lib
opencv_ximgproc343d.lib
opencv_xobjdetect343d.lib
opencv_xphoto343d.lib
opencv_aruco343.lib
opencv_bgsegm343.lib
opencv_bioinspired343.lib
opencv_calib3d343.lib
opencv_ccalib343.lib
opencv_core343.lib
opencv_datasets343.lib
opencv_dnn343.lib
opencv_dnn_objdetect343.lib
opencv_dpm343.lib
opencv_face343.lib
opencv_features2d343.lib
opencv_flann343.lib
opencv_fuzzy343.lib
opencv_hfs343.lib
opencv_highgui343.lib
opencv_imgcodecs343.lib
opencv_imgproc343.lib
opencv_img_hash343.lib
opencv_line_descriptor343.lib
opencv_ml343.lib
opencv_objdetect343.lib
opencv_optflow343.lib
opencv_phase_unwrapping343.lib
opencv_photo343.lib
opencv_plot343.lib
opencv_reg343.lib
opencv_rgbd343.lib
opencv_saliency343.lib
opencv_shape343.lib
opencv_stereo343.lib
opencv_stitching343.lib
opencv_structured_light343.lib
opencv_superres343.lib
opencv_surface_matching343.lib
opencv_text343.lib
opencv_tracking343.lib
opencv_ts343.lib
opencv_video343.lib
opencv_videoio343.lib
opencv_videostab343.lib
opencv_xfeatures2d343.lib
opencv_ximgproc343.lib
opencv_xobjdetect343.lib
opencv_xphoto343.lib
至此,opencv的配置完成了,我們需要通過測試來檢查是否安裝配置成功。
五、測試程式
在新建的工程中新增如下程式碼,生成解決方案。
#include "opencv2/opencv.hpp"
#include"opencv2/xfeatures2d.hpp"
#include<opencv2/xfeatures2d/nonfree.hpp>
using namespace cv;
using namespace std;
int main() {
Mat img = imread("1.png", CV_LOAD_IMAGE_COLOR);
Ptr<Feature2D> sift = xfeatures2d::SIFT::create();
vector<KeyPoint> keypoints;
Mat descriptors;
sift->detectAndCompute(img, noArray(), keypoints, descriptors);
drawKeypoints(img, keypoints, descriptors, Scalar(0, 255, 255));
imshow("Result", descriptors);
waitKey(0);
return 0;
}
顯示如下圖表示測試成功。
參考部落格:https://blog.csdn.net/weixin_42012977/article/details/82992962