提取ORB特征点进行特征匹配
高翔大佬的代码用的是opencv3,所以下面两句有点不一样~如果使用opencv2,则是create("ORB"),否则报错。
Ptr<FeatureDetector> detector =ORB::create(); //创建ORB特征点检测
Ptr<DescriptorExtractor> descriptor =ORB::create(); //使用ORB特征来描述特征点
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc,char** argv){
if(argc!=3){
cout<<"usage:feature_extraction img1,img2"<<endl;
return -1;
}
//读取图像
Mat img_1=imread(argv[1],CV_LOAD_IMAGE_COLOR);
Mat img_2=imread(argv[2],CV_LOAD_IMAGE_COLOR);
//初始化
std::vector<KeyPoint> keypoints_1,keypoints_2; //创建关键点
Mat descriptors_1,descriptors_2; //创建描述子
//opencv2中create()要加内容
Ptr<FeatureDetector> detector =ORB::create("ORB"); //创建ORB特征点检测
Ptr<DescriptorExtractor> descriptor =ORB::create("ORB"); //使用ORB特征来描述特征点
Ptr<DescriptorMatcher> matcher=DescriptorMatcher::create("BruteForce-Hamming");//设置特征匹配使用的汉明距离计算
//设置特征点检测
//Ptr<FeatureDetector> detector = FeatureDetector::create(dectector_name);
//Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create(descriptor_name);
//第一步:检测oriented FAST角点检测
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);
Mat outimg1;
drawKeypoints(img_1,keypoints_1,outimg1,Scalar::all(-1),DrawMatchesFlags::DEFAULT);
imshow("ORB feature points",outimg1);
waitKey(0);
//第三步:对两幅图像中的BRIEF描述子进行匹配,使用hamming距离
vector<DMatch> matches;
matcher->match(descriptors_1,descriptors_2,matches);
//第四步:绘制所有匹配结果
Mat img_match;
drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_match);
imshow("All match point pairs",img_match);
waitKey(0);
//第五步:匹配点对筛选
double min_dist=10000,max_dist=0;//最小10000,最大0,通过反向计算求得max和min
//找出匹配点之间的最小距离和最大距离
for(int i=0;i<descriptors_1.rows;i++){
double dist=matches[i].distance;
if(dist<min_dist) min_dist=dist;
if(dist>max_dist) max_dist=dist;
}
// 仅供娱乐的写法
//min_dist = min_element( matches.begin(), matches.end(), [](const DMatch& m1, const DMatch& m2) {return m1.distance<m2.distance;} )->distance;
//max_dist = max_element( matches.begin(), matches.end(), [](const DMatch& m1, const DMatch& m2) {return m1.distance<m2.distance;} )->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_goodMatch;
drawMatches(img_1,keypoints_1,img_2,keypoints_2,good_matches,img_goodMatch);
imshow("Match point pairs after optimization",img_goodMatch);
waitKey(0);
return 0;
}
CMakeLists.txt
cmake_minimum_required( VERSION 2.8 )
project( vo1 )
set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++11 -O3" )
#opencv2
find_package( OpenCV REQUIRED )
include_directories(
${OpenCV_INCLUDE_DIRS}
${CSPARSE_INCLUDE_DIR}
"/usr/include/eigen3/"
)
add_executable( feature_extraction feature_extraction.cc )
target_link_libraries( feature_extraction ${OpenCV_LIBS} )
运行结果:
ORB特征点图
显示所有匹配点
筛选过后的匹配点