opencv的编译与配置参考:https://www.cnblogs.com/fcfc940503/p/11502930.html
记得使用cmak进行配置时,需要勾选OPENCV_ENABLE_NOFREE,要不是后面使用SURF算法是会报错的。另外就是如果是debug就引用debug的opencv lib,如果是realse就引用opencv realse的lib,不要引错了。
#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include<opencv2/xfeatures2d.hpp>
#include<opencv2/xfeatures2d/nonfree.hpp>
#include <iostream>
using namespace cv;
using namespace cv::xfeatures2d;
using namespace std;
const int HasValue = 1500;
int main()
{
Mat image01 = imread("C:\\Users\\wu\\Pictures\\1.jpg", 1); //右图
Mat image02 = imread("C:\\Users\\wu\\Pictures\\2.jpg", 1); //左图
namedWindow("p2", 0);
namedWindow("p1", 0);
imshow("p2", image01);
imshow("p1", image02);
//灰度图转换
Mat image1, image2;
cvtColor(image01, image1, 7);
cvtColor(image02, image2, 7);
//提取特征点
Ptr<cv::xfeatures2d::SURF> detector = cv::xfeatures2d::SURF::create(HasValue);
// ; // 海塞矩阵阈值,在这里调整精度值越大点越少,越精准
vector<KeyPoint> keyPoint1, keyPoint2;
detector->detect(image1, keyPoint1);
detector->detect(image2, keyPoint2);
//特征点描述,为下边的特征点匹配做准备
Ptr <cv::xfeatures2d::SURF> SurfDescriptor = cv::xfeatures2d::SURF::create(HasValue);
Mat imageDesc1, imageDesc2;
SurfDescriptor->compute(image1, keyPoint1, imageDesc1);
SurfDescriptor->compute(image2, keyPoint2, imageDesc2);
//获得匹配特征点,并提取最优配对
FlannBasedMatcher matcher;
vector<DMatch> matchePoints;
matcher.match(imageDesc1, imageDesc2, matchePoints, Mat());
cout << "total match points: " << matchePoints.size() << endl;
Mat img_match;
drawMatches(image01, keyPoint1, image02, keyPoint2, matchePoints, img_match);
cv::namedWindow("match", 0);
cv::imshow("match", img_match);
cv::imwrite("match.jpg", img_match);
waitKey();
return 0;
}