FLANN是快速最近邻搜索包(Fast_Library_for_Approximate_Nearest_Neighbors)的简称。它是一个对大数据集和高维特征进行最近邻搜索的算法的集合,而且这些算法都已经被优化过了。在面对大数据集是它的效果要好于BFMatcher。
使用FLANN匹配,我们需要传入两个字典作为参数。这两个用来确定要使用的算法和其他相关参数等。
FLANN是一个单独的算法库,opencv中引用了,具体的算法以及上面的参数k-d树算法还没研究,目前只学习下opencv中的使用方法
//29 flann匹配
void StartOp2::ImageProcess2_29()
{
Mat img1 = imread("../../Images/22.jpg", IMREAD_GRAYSCALE);
Mat img2 = imread("../../Images/23.jpg", IMREAD_GRAYSCALE);
if (!img1.data || !img2.data) {
}
imshow("image1", img1);
imshow("image2", img2);
int minHessian = 400;
Ptr<SURF> detector = SURF::create(minHessian);
vector<KeyPoint> keypoints_1;
vector<KeyPoint> keypoints_2;
Mat descriptor_1, descriptor_2;
detector->detectAndCompute(img1, Mat(), keypoints_1, descriptor_1);
detector->detectAndCompute(img2, Mat(), keypoints_2, descriptor_2);
//matching
FlannBasedMatcher matcher;
vector<DMatch> matches;
matcher.match(descriptor_1, descriptor_2, matches);
//find good matched points
double minDist = 1000;
double maxDist = 0;
for (int i=0;i<descriptor_1.rows;i++)
{
double dist = matches[i].distance;
if (dist>maxDist)
{
maxDist = dist;
}
if (dist<minDist)
{
minDist = dist;
}
}
vector<DMatch> goodMatches;
for (int i=0; i<descriptor_1.rows; i++)
{
double dist = matches[i].distance;
if (dist<max(3*minDist,0.02))
{
goodMatches.push_back(matches[i]);
}
}
Mat matchesImage;
drawMatches(img1, keypoints_1,img2,keypoints_2,goodMatches,matchesImage,Scalar::all(-1),Scalar::all(-1),vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("output",matchesImage);
}
参考文章:
https://blog.csdn.net/qq_36387683/article/details/80578480
https://blog.csdn.net/cshilin/article/details/52107580