c++和opencv小知识:ORB特征点匹配小流程
·
ORB特征点匹配小流程
#include <stdio.h>
#include <iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
//读取图像
Mat img1 = imread("1.jpg");
Mat img2 = imread("2.jpg");
//初始化
vector<KeyPoint> keypoints_1, keypoints_2;
Mat description_1, description_2;
Ptr<ORB> orb = ORB::create(500,1.2f,8,31,0,2,ORB::HARRIS_SCORE,31,20);
//第一步选择角点
orb->detect(img1,keypoints_1);
orb->detect(img1, keypoints_2);
//第二步计算描述子
orb->compute(img1,keypoints_1,description_1);
orb->compute(img2, keypoints_2, description_2);
//显示找到的角点
Mat outimg1;
drawKeypoints(img1,keypoints_1,outimg1,Scalar::all(-1),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
imshow("ORB特征点", outimg1);
//第三步对图像的描述子进行匹配
vector<DMatch> matches;
BFMatcher matcher(NORM_HAMMING);
matcher.match(description_1,description_2,matches);
//第四步对匹配点进行筛选
double min_dist = 10000, max_dist = 0;
//找出所有匹配中最小距离和最大距离
for (int i = 0; i < description_1.rows; i++)
{
double dist = matches[i].distance;
if (dist < min_dist) { min_dist = dist; }
if (dist > max_dist) { max_dist = dist; }
}
cout << min_dist <<"\t"<< max_dist<<endl;
//当描述子之间的距离大于两倍的最小距离时,即认为匹配错误
//但有时候最小距离会非常小,需要设置一个经验值作为下限
vector<DMatch> good_match;
for (int j = 0; j < description_1.rows; j++)
{
if (matches[j].distance<=max(2*min_dist,30.0))
{
good_match.push_back(matches[j]);
}
}
//第五步显示匹配结果
Mat img_goodmatch;
drawMatches(img1, keypoints_1, img2, keypoints_2, good_match, img_goodmatch);
imshow("优化后的匹配", img_goodmatch);
waitKey(0);
return 0;
}
更多推荐
所有评论(0)