opencv_C++ FlannBasedMatcher() FLANN特征匹配
FlannBasedMatcher中FLANN的含义是Fast Library forApproximate Nearest Neighbors,从字面意思可知它是一种近似法,算法更快但是找到的是最近邻近似匹配,所以当我们需要找到一个相对好的匹配但是不需要最佳匹配的时候往往使用FlannBasedMatcher。当然也可以通过调整FlannBasedMatcher的参数来提高匹配的精度或者提高算法速度,但是相应地算法速度或者算法精度会受到影响。
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
int main()
{
Mat srcImage = imread("curry_dlt.jpg");
Mat dstImage = imread("curry1.jpg");
// surf 特征提取
int minHessian = 450;
Ptr<SURF> detector = SURF::create(minHessian);
vector<KeyPoint> keypoints_src;
vector<KeyPoint> keypoints_dst;
Mat descriptor_src, descriptor_dst;
detector->detectAndCompute(srcImage, Mat(), keypoints_src, descriptor_src);
detector->detectAndCompute(dstImage, Mat(), keypoints_dst, descriptor_dst);
// matching
FlannBasedMatcher matcher;
vector<DMatch> matches;
matcher.match(descriptor_dst, descriptor_src, matches);
// find good matched points
double minDist = 0, maxDist = 0;
for (size_t i = 0; i < matches.size(); i++)
{
double dist = matches[i].distance;
if (dist > maxDist)
maxDist = dist;
if (dist < minDist)
minDist = dist;
}
vector<DMatch> goodMatches;
for (size_t i = 0; i < matches.size(); i++)
{
double dist = matches[i].distance;
if (dist < max(3 * minDist, 0.02))
{
goodMatches.push_back(matches[i]);
}
}
Mat matchesImage;
drawMatches(dstImage, keypoints_dst, srcImage, keypoints_src, goodMatches, matchesImage, Scalar::all(-1), \
Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("matchesImage", matchesImage);
waitKey(0);
return 0;
}