VS2019編譯opencv4.1.2(帶sift等額外算法)

編譯opencv4.1.2

opencv4中的sift、surf受專利保護,不能直接使用,所以需要自己動手編譯相關的庫。

1. 下載opencv與opencv-contrib代碼,其中下載的版本爲4.1.2,另外還需要cmake工具。它們具體的下載地址如下:

Opencv: https://github.com/opencv/opencv/releases

Opencv_contrib: https://github.com/opencv/opencv_contrib/releases

Cmake: https://cmake.org/download/

2. make工作,需要編譯opencv與opencv-contrib。添加額外的功能,如sift和surf等收費功能,需要勾選OPENCV_ENABLE_NONFREE,配置OPENCV_EXTRA_MODULES_PATH。

於是opencv4.1.2-cmake中就有相關的編譯文件。

注意:在configure過程中會出現問題,比如找不到ippicv、ffmpeg與xfeatures2d相關文件,主要的原因是網址訪問錯誤導致的原因,服務器連接不上需要修改:

把raw.githubusercontent.com修改爲raw.staticdn.net

具體修改的cmake文件

*\opencv_contrib-4.1.2\modules\xfeatures2d\cmake:

download_boostdesc.cmake   download_vgg.cmake

*\opencv-4.1.2\3rdparty\ffmpeg:ffmpeg.cmake

*\opencv-4.1.2\3rdparty\ippicv: ippicv.cmake

3. vs編譯。打開make工作輸出的文件目錄,用vs打開OpenCV.sln工程,點擊生成->批生成->選中ALL_BUILD與INSTALL,然後生成。(編譯生成需要花一定的時間)

 

PS: 編譯過程中會找不到python37_d.lib或python36_d.lib或python27_d.lib。

需要打開opencv工程中的binding/opencv_python3/外部依賴項/pyconfig.h,修改python37_d.lib爲python37.lib,已經註釋掉 #Py_DEBUG

4. 編譯結果驗證。編譯成功之後make目錄中就會有install目錄,該目錄就是opencv的函數庫。

First of all:配置opencv的環境變量,在PATH中添加*\cmake\install\x64\vc16\bin

(1) 創建空的C++工程;

(2) 打開該工程的屬性,配置VC++目錄中的包含目錄與庫目錄

 

(3) 鏈接器->輸入->添加依賴庫,這裏爲*\opencv4.1.2-cmake\install\x64\vc16\lib\的lib文件,其中release是不帶d的lib文件,debug是帶d的lib文件。

PS: 文件比較多,可以在cmd中輸入dir /b *d.lib>debug.txt  dir /b *412.lib>release.txt,方可得到debug和release模式不同的依賴項。如果覺得複製這些lib名稱比較麻煩,還可以將這些lib編譯成一個lib文件,也就是opencv_world,因此需要在cmake中勾選BUILD_opencv_world。

5. 測試sift特徵

(1) 創建.cpp相關代碼文件;

(2) 代碼如下:

#include <opencv2/opencv.hpp>
#include <iostream>


int main(int argc, char** argv) {
	cv::Mat box = imread("1.png");
	cv::Mat box_in_sence = imread("2.png");

	// 創建SIFT對象
	cv::xfeatures2d::SIFT detector = cv::xfeatures2d::SIFT::create();
	std::vector<KeyPoint> kpts_01, kpts_02;
	cv::Mat descriptors1, descriptors2;

	detector->detectAndCompute(box, cv::Mat(), kpts_01, descriptors1);
	detector->detectAndCompute(box_in_sence, cv::Mat(), kpts_02, descriptors2);
	// 定義描述子匹配 - 暴力匹配
	cv::Ptr<cv::DescriptorMatcher> matcher = Dcv::escriptorMatcher::create(cv::DescriptorMatcher::BRUTEFORCE);
	std::vector< cv::DMatch > matches;
	matcher->match(descriptors1, descriptors2, matches);
	// 繪製匹配
	cv::Mat img_matches;
	cv::drawMatches(box, kpts_01, box_in_sence, kpts_02, matches, img_matches);
	cv::imshow("Matches", img_matches);
	cv::imwrite("D:/result.png", img_matches);
	cv::waitKey(0);
	return 0;
}

(3) 效果圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章