#include <opencv2/xfeatures2d/nonfree.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <iostream> using namespace cv;
using namespace std; int main(int argc, char** argv)
{
Mat img1 = imread(argv[]);
Mat img2 = imread(argv[]);
vector<KeyPoint> keypoints_1, keypoints_2;
Mat descriptor_1, descriptor_2;
Ptr<Feature2D> sift = xfeatures2d::SIFT::create(, , 0.04, ); sift->detectAndCompute(img1, noArray(), keypoints_1, descriptor_1);
sift->detectAndCompute(img2, noArray(), keypoints_2, descriptor_2);
cout<< keypoints_1.size()<<" "<<keypoints_2.size()<<endl;
Mat outimg1;
drawKeypoints(img1, keypoints_1, outimg1, Scalar::all(-), DrawMatchesFlags::DEFAULT);
imshow("KeyPoint", outimg1); vector<DMatch> matches;
vector<vector<DMatch>> knn_matches; BFMatcher matcher(NORM_L2);
matcher.knnMatch(descriptor_1, descriptor_2, knn_matches, ); for (size_t r = ; r < knn_matches.size(); ++r)
{
if (knn_matches[r][].distance > 0.8*knn_matches[r][].distance ) continue;
matches.push_back(knn_matches[r][]);
} Mat img_match;
Mat img_goodmatch;
drawMatches (img1, keypoints_1, img2, keypoints_2, matches, img_goodmatch);
imshow("good match", img_goodmatch);
waitKey(); return ; }
输入两张图像
提取sift特征点
使用knnmatch进行最近邻匹配