find_feature_matches
vector<KeyPoint> keypoints_1,keypoints_2;
vector<DMatch> matches;
find_feature_matches(img1,img2,keypoints_1,keypoints_2,matches);
- 定义
void find_feature_matches ( const Mat& img1, const Mat& img2,
vector<KeyPoint>& kp1,
vector<KeyPoint>& kp2,
vector<DMatch>& matches)
{
Mat dp1,dp2;
Ptr<FeatureDetector> detector = ORB::create();
Ptr<DescriptorExtractor> descriptor = ORB::create();
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
detector->detect (img1, kp1);
detector->detect (img2, kp2);
descriptor->compute( img1, kp1, dp1);
descriptor->compute( img2, kp2, dp2);
vector<DMatch> match;
matcher->match( dp1, dp2, match);
double min_d = 10000,max_d = 0;
for (int i = 0; i < dp1.rows; ++i) {
double dist = match[i].distance;
if(dist < min_d) min_d = dist;
if(dist > max_d) max_d = dist;
}
printf("max dist : %f \n",max_d);
printf("min dist : %f \n",min_d);
for (int i =0; i < dp1.rows;++i) {
if (match[i].distance <= max( 2*min_d, 30.0 ))
matches.push_back(match[i]);
}
}
- KeyPoint类
//默认构造函数
CV_WRAP KeyPoint():pt(0,0),size(0),angle(-1),response(0),octave(0),class_id(-1){}
//pt(x,y):关键点坐标
//size():该关键点邻域直径大小
//angle:角度,表示关键点的方向,值为【0,360】,负值表示不使用
//response:响应强度
//octave:从哪一层金字塔得到的关键点
//class_id:当要对图片进行分类时,用class_id对每个关键点进行区分,默认为-1
关于keypoints_1[0],keypoints_2[0],matches[]:
keypoints_1[0]:存储图一的第0个特征点,不一定与keypoints_2[0]对应
keypoints_2[0]:存储图二的第0个特征点,不一定与keypoints_2[0]对应
matches[0]:存储某一特征点的信息,图一中第matches.queryIdx个特征点与图二中第matches.trainIdx个特征点是相互对应的,matches.distance是两个对应特征点之间的距离。