简介
Flann-based matcher (cv::FlannBasedMatcher),使用快速近似最近邻搜索算法寻找。
相关函数
代码展示
# include<opencv2\opencv.hpp>
# include<opencv2/xfeatures2d.hpp>
# include <iostream>
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
double t1 = (double)getTickCount();
int main(int argc, char** argv) {
Mat src1,src2;
src1 = imread("E:/tuku/nihuai1.jpg",IMREAD_GRAYSCALE);
src2 = imread("E:/tuku/nihuai2.jpg", IMREAD_GRAYSCALE);
if (src1.empty()||src2.empty()) {
cout << "can't find this picture...";
return -1;
}
imshow("input1", src1);
imshow("input2", src2);
//surf feature extraction
int minHessian = 400;
Ptr<SURF> detector = SURF::create(minHessian);
vector<KeyPoint>keypoint_1;
vector<KeyPoint>keypoint_2;
Mat descriptor_1, descriptor_2;
//计算特征点和描述子
detector->detectAndCompute(src1, Mat(), keypoint_1, descriptor_1);
detector->detectAndCompute(src2, Mat(), keypoint_2, descriptor_2);
//match
FlannBasedMatcher matcher;
vector<DMatch>matchers;
matcher.match(descriptor_1, descriptor_2, matchers);
//find good matched points 寻找最大与最小距离
double maxDist = 1000;
double minDist = 0;
for (int row = 0; row < descriptor_1.rows; row++) {
double dist = matchers[row].distance;
if (dist > maxDist)
{
maxDist = dist;
}
if (dist < minDist)
{
minDist = dist;
}
}
printf("max distance :%f\n", maxDist);
printf("min distance :%f\n", minDist);
vector<DMatch>goodmatchers;
for (int row = 0; row < descriptor_1.rows; row++) {
double dist = matchers[row].distance;
if (dist < max(2* minDist, 0.04)) {
goodmatchers.push_back(matchers[row]);
}
}
Mat MatchImage;
drawMatches(src1, keypoint_1, src2, keypoint_2, goodmatchers, MatchImage, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("out put", MatchImage);
//计算时间
double t2= (double)getTickCount();
double t = (t2 - t1)/ getTickFrequency();
printf("time :%f\n", t);
waitKey(0);
return 0;
}