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;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值