由于SURF等函数存在版权问题,在opencv4的版本中无法直接使用,需要使用第三方库才行,本人在opencv4.4.0版本上进行编译了。
下载opencv4.4.0源码
下载opencv_contrib4.4.0
然后按照此步骤安装
使用案例
#include <iostream>
#include<opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv4/opencv2/xfeatures2d.hpp>
using namespace std;
using namespace cv;
using namespace xfeatures2d;
int main()
{
Mat srcImage1 = imread("93.jpg",IMREAD_GRAYSCALE);
Mat srcImage2 = imread("95.jpg",IMREAD_GRAYSCALE);
//判断文件是否读取成功
if (srcImage1.empty() || srcImage2.empty())
{
cout << "图像加载失败!";
return -1;
}
else
cout << "图像加载成功..." << endl << endl;
//检测两幅图像中的特征点
int minHessian = 2000;
Ptr<cv::xfeatures2d::SURF> detector = cv::xfeatures2d::SURF::create(minHessian);
// SurfFeatureDetector detector(minHessian); //老版本
vector<KeyPoint>keypoint1, keypoint2;
detector->detect(srcImage1, keypoint1);
detector->detect(srcImage2, keypoint2);
//计算特征向量的描述子
Ptr <cv::xfeatures2d::SURF> descriptorExtractor = cv::xfeatures2d::SURF::create(minHessian);;
Mat descriptors1, descriptors2;
descriptorExtractor->compute(srcImage1, keypoint1, descriptors1);
descriptorExtractor->compute(srcImage2, keypoint2, descriptors2);
//使用BruteForceMatcher进行描述符匹配
BFMatcher matcher(NORM_L2);
vector<DMatch>matches;
matcher.match(descriptors1, descriptors2, matches);
//绘制匹配特征点
Mat matchImage;
drawMatches(srcImage1, keypoint1, srcImage2, keypoint2, matches, matchImage);
//显示匹配的图像
namedWindow("Match", WINDOW_AUTOSIZE);
imshow("Match", matchImage);
waitKey(0);
return 0;
}
编译命令
g++ -I/usr/local/include/opencv4/ surf1.cpp -o surf1 -L/usr/local/lib/ -lopencv_features2d -lopencv_xfeatures2d -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -lopencv_photo
结果