用同样的两张图进行两次匹配,用FlannBasedMatcher匹配器进行匹配,两次的匹配点数量为什么会不一样?希望大神解答下,万分感谢!!代码如下:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "imgproc/imgproc.hpp"
#include <opencv2/features2d/features2d.hpp>
#include "opencv2/xfeatures2d/nonfree.hpp"
#include <vector>
#include "opencv2/opencv.hpp"
#include <cmath>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
Mat image02 = imread("D:\\Chrome Download\\dataset\\CAB\\P1010477.JPG");
Mat image01 = imread("D:\\Chrome Download\\dataset\\CAB\\P1010478.JPG");
//灰度图转换
Mat image1, image2;
cvtColor(image01, image1, CV_BGR2GRAY);
cvtColor(image02, image2, CV_BGR2GRAY);
//提取特征点
int minHessian = 1000;//2000
Ptr<xfeatures2d::SURF> surfDetector = xfeatures2d::SURF::create(minHessian);
vector<KeyPoint> keyPoint1, keyPoint2, keyPoint3, keyPoint4;
vector<Point2f> imagePoints1, imagePoints2;
surfDetector->detect(image1, keyPoint1);
surfDetector->detect(image2, keyPoint2);
//特征点描述,为下边的特征点匹配做准备
Mat imageDesc1, imageDesc2;
surfDetector->compute(image1, keyPoint1, imageDesc1);
surfDetector->compute(image2, keyPoint2, imageDesc2);
//获得匹配特征点,并提取最优配对
FlannBasedMatcher matcher;
vector<vector<DMatch> > InitMatchePoints1, InitMatchePoints2;
vector<DMatch> GoodMatchePoints1, GoodMatchePoints2;
matcher.knnMatch(imageDesc1, imageDesc2, InitMatchePoints1, 2);
//Lowe's algorithm,获取优秀匹配点
for (int i = 0; i < InitMatchePoints1.size(); i++)
{
if (InitMatchePoints1[i][0].distance < 0.8 * InitMatchePoints1[i][1].distance)
{
GoodMatchePoints1.push_back(InitMatchePoints1[i][0]);
}
}
cout << "匹配点的个数: " << GoodMatchePoints1.size() << endl;
matcher.knnMatch(imageDesc1, imageDesc2, InitMatchePoints2, 2);
//Lowe's algorithm,获取优秀匹配点
for (int i = 0; i < InitMatchePoints2.size(); i++)
{
if (InitMatchePoints2[i][0].distance < 0.8 * InitMatchePoints2[i][1].distance)
{
GoodMatchePoints2.push_back(InitMatchePoints2[i][0]);
}
}
cout << "匹配点的个数: " << GoodMatchePoints2.size() << endl;
return 0;
}
|