FLANN进行特征点匹配
程序员文章站
2022-06-11 12:40:29
...
opencv3使用FLANN进行特征点匹配
废话不多说,直接上代码:`#include<opencv2\opencv.hpp>
#include<opencv2\xfeatures2d.hpp>
#include
using namespace cv;
using namespace xfeatures2d;
using namespace std;
int main()
{
Mat src1 = imread(“1.jpg”,1);
Mat src2 = imread(“2.jpg”,1);
namedWindow(“input1”, WINDOW_NORMAL);
imshow(“input1”, src1);
imshow(“input2”, src2);
int minHessian = 400;
Ptrsurf = SURF::create(minHessian);
vectorkeypoint1, keypoint2;
Mat descriptors1, descripors2;
surf->detectAndCompute(src1, Mat(), keypoint1, descriptors1);
surf->detectAndCompute(src2, Mat(), keypoint2, descripors2);
FlannBasedMatcher matcher;
vector<DMatch>matches;
matcher.match(descriptors1, descripors2, matches);
double minDist = 1000;
double maxDist = 0;
for (int i = 0; i < descriptors1.rows; i++)
{
double dist = matches[i].distance;
if (dist > maxDist)
{
maxDist = dist;
}
if (dist < minDist)
{
minDist = dist;
}
}
printf("max distance:%1f\n", maxDist);
printf("min distance:%1f\n", minDist);
vector<DMatch>goodMatchers;
for (int i = 0; i < descriptors1.rows; i++)
{
double dist = matches[i].distance;
if (dist < max(2 * minDist, 0.15))
{
goodMatchers.push_back(matches[i]);
}
}
Mat matchimg;
drawMatches(src1, keypoint1, src2, keypoint2, goodMatchers, matchimg, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
for (int i = 0; i < goodMatchers.size(); i++)
{
printf(">符合条件的匹配点[%d] 特征点1:%d--特征点2:%d \n", i, goodMatchers[i].queryIdx, goodMatchers[i].trainIdx);
}
namedWindow("output1", WINDOW_NORMAL);
imshow("output1", matchimg);
waitKey(0);
return 0;
}`
截图如下: