当前位置:网站首页>Opencv学习之ORB特征提取和匹配
Opencv学习之ORB特征提取和匹配
2022-08-04 12:29:00 【郑学长】
**
Opencv学习之ORB特征提取和匹配
**
编译环境:VS2017 + Opencv3.4.7
注:ORB算法是Opencv3之后才有的。
ORB特征的介绍:ORB特征是由关键点和描述子两部分组成。
提取ORB特征分为如下两个步骤:
1.FAST角点提取:FAST是一种角点,主要检测局部像素灰度变化明显的地方。找出图像中的“角点”,相较于原版的FAST,ORB中计算了特征点的主方向,为后续的BRIEF描述子增加了旋转不变特性。
2.BRIEF描述子:BRIEF是一种二进制描述子,其描述向量由许多0和1组成,需要用汉明距离度量,对提取出的特征点的周围图像区域进行描述,使用先前计算的方向信息。
特征匹配:简单的暴力匹配,对每个特征点与所有的待匹配点,测量它们描述子之间的距离,然后排序,取最近的一个作为匹配点。描述子距离表示了两个特征之间的相似程度,两个二进制串之间的汉明距离,指的是其不同位数的个数。
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <chrono>
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
//读取图像
Mat img_1 = imread("../Images/L10.jpg", CV_LOAD_IMAGE_COLOR);
Mat img_2 = imread("../Images/R10.jpg", CV_LOAD_IMAGE_COLOR);
assert(img_1.data != nullptr && img_2.data != nullptr);
//初始化
std::vector<KeyPoint> keypoints_1, keypoints_2;
Mat descriptors_1, descriptors_2;
Ptr<FeatureDetector> detector = ORB::create();
Ptr<FeatureDetector> descriptor = ORB::create();
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
//第一步,检测Oriented FAST 角点位置
chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
detector->detect(img_1, keypoints_1);
detector->detect(img_2, keypoints_2);
//第二部,根据角点位置计算BRIEF描述子
descriptor->compute(img_1, keypoints_1, descriptors_1);
descriptor->compute(img_2, keypoints_2, descriptors_2);
chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<doub``le>>(t2 - t1);
cout << "extract ORB cost = " << time_used.count() << "seconds." << endl;
Mat outimg1;
drawKeypoints(img_1, keypoints_1, outimg1, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
imshow("ORB features", outimg1);
//第三步:对两幅图像中打BRIEF描述子进行匹配,使用Hamming距离
vector<DMatch> matches;
t1 = chrono::steady_clock::now();
matcher->match(descriptors_1, descriptors_2, matches);
t2 = chrono::steady_clock::now();
time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
cout << "match ORB cost = " << time_used.count() << "seconds." << endl;
//第四步:匹配点对筛选 计算最小距离和最大距离
auto min_max = minmax_element(matches.begin(), matches.end(), [](const DMatch &m1, const DMatch &m2) { return m1.distance < m2.distance; });
double min_dist = min_max.first->distance;
double max_dist = min_max.second->distance;
printf("-- Max dist : &f \n", max_dist);
printf("-- Min dist : &f \n", min_dist);
//当描述子之间的距离大于两倍的最小距离时,即认为匹配有误.但有时候最小距离会非常小,设置一个经验值30作为下限.
std::vector<DMatch> good_matches;
for (int i = 0; i < descriptors_1.rows; ++i) {
if (matches[i].distance <= max(2 * min_dist, 30.0)) {
good_matches.push_back(matches[i]);
}
}
//第五步,绘制匹配结果
Mat img_match;
Mat img_goodmatch;
drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_match);
drawMatches(img_1, keypoints_1, img_2, keypoints_2, good_matches, img_goodmatch);
imshow("all matches", img_match);
imwrite("../Result/img_match_1.jpg", img_match);
imshow("good matches", img_goodmatch);
imwrite("../Result/img_goodmatch_1.jpg", img_goodmatch);
waitKey(0);
return 0;
}
边栏推荐
- 形态学(膨胀、腐蚀)
- A Collection of Flutter Tutorials (2022 Edition)
- Analysis and comparison of mobile cross-end technical solutions
- 电源测试之输出动态响应(Output Dynamic Response Test)
- LeetCode Daily Question (858. Mirror Reflection)
- Motion Regulations (18) - and check the basic questions - gang
- 新SCADA 系统:集成边缘计算、MQTT 和云平台
- 来 TDengine 开发者大会,洞悉数据技术发展的未来趋势
- DateTimeFormatter api
- 划重点!2022面试必刷461道大厂架构面试真题汇总+面经+简历模板
猜你喜欢
随机推荐
如何治理资源浪费?百度云原生成本优化最佳实践
Focusing on data sources, data quality and model performance to build a credit profile of small and micro enterprises
oracle sql中根据条件判断是否插入数据
两个数组中用第二个数组的Value对比换第一个数组中的Key
外置USB供电与内置锂电池供电自动切换电路
OAuth2图文快速入门
Motion Rule (16)-Union Check Basic Questions-Relations
“蔚来杯“2022牛客暑期多校训练营2 G、J、K
常用代码模板1——基础语法
移动跨端技术方案分析对比
Nacos手摸手教学【二】Nacos注册中心
业务中我们如何更新缓存?Redis
Geoffrey Hinton:深度学习的下一个大事件
Flutter强大的下拉筛选菜单gzx_dropdown_menu
ES 节点2G内存分析
划重点!2022面试必刷461道大厂架构面试真题汇总+面经+简历模板
Hands-on Deep Learning_LeNet
Programmer Qixi Gift - How to quickly build an exclusive chat room for your girlfriend in 30 minutes
Practical sharing of distributed link tracking Jaeger + microservice Pig on Rainbond
Oracle 19c 单实例 19.3.0 升级到19.11.0 详细教程