基于opencv的特征点匹配法

基于opencv的特征点匹配法

基于opencv的特征点匹配方法已经有很多博文了
我也是参考了其中几篇
给出链接供大家参考
Opencv2.4.9源码分析——SIFT
Opencv学习笔记(六)SURF学习笔记
还有一篇链接居然失效了~囧
不过我看他们的文章基本是基于其中一个算法的(比如SIFT),我这个代码中给出了使用不同算子特征的方法。

#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()

{
double time = (double)cvGetTickCount();
Mat image1=imread("0.jpg");
Mat image2=imread("1.jpg");
//Mat image1=imread("1.jpg");

//Mat image2=imread("2.jpg");
Mat reimage1;
Mat reimage2;
//undistortion
//相机内参
Mat intrinsic = (Mat_<double>(3,3) << 621.1558, 0, 327.3255, 0, 618.2676, 238.3404, 0, 0, 1.0000);
//cout<<intrinsic<<endl;
//相机畸变参数
Mat distortion= (Mat_<double>(4,1) << 0.100995533983367 ,-0.179776019431878 , 0.000775515546919460, -0.00493615870309803);
//这里是去畸变,当然需要按照你自己的摄像机调整内参和畸变参数,reimage是去畸变后的图片
undistort(image1,reimage1,intrinsic,distortion);
undistort(image2,reimage2,intrinsic,distortion);
//imshow("undistortion",reimage1);
// 检测特征点

vector<KeyPoint> keypoints1,keypoints2;   
//在这里切换不同的detector
//OrbFeatureDetector detector(200);
SurfFeatureDetector detector(30);
//FastFeatureDetector detector(25);
//SurfFeatureDetector detector(50);
//detector.detect(reimage1, keypoints1);
//detector.detect(reimage2, keypoints2);
detector.detect(image1, keypoints1);
detector.detect(image2, keypoints2);

// 描述特征点
//这里选择不同的descriptor
//OrbDescriptorExtractor Desc;
SurfDescriptorExtractor Desc;
//SiftDescriptorExtractor Desc;
//BriefDescriptorExtractor Desc;
Mat descriptros1,descriptros2;

//Desc.compute(reimage1,keypoints1,descriptros1);
//Desc.compute(reimage2,keypoints2,descriptros2);
Desc.compute(image1,keypoints1,descriptros1);
Desc.compute(image2,keypoints2,descriptros2);
// 计算匹配点数

BruteForceMatcher<L2<float> >matcher;

vector<DMatch> matches;

matcher.match(descriptros1,descriptros2,matches);
//调整topN个点
int n=100;
std::nth_element(matches.begin(),matches.begin()+n,matches.end());

matches.erase(matches.begin()+n+1,matches.end());

// 画出匹配图
Mat imageMatches;
//drawMatches(reimage1,keypoints1,reimage2,keypoints2,matches,imageMatches,Scalar(255,0,0));
drawMatches(image1,keypoints1,image2,keypoints2,matches,imageMatches,Scalar(255,0,0));
//namedWindow("Match");
//  time count
time = (double)cvGetTickCount() - time;
//输出运行时间
printf( "run time = %gms\n", time/(cvGetTickFrequency()*1000) );
imshow("Match",imageMatches);
//waitkey不要忘记哦
waitKey();



return 0;

}

ps:第一次写博客,格式还比较烂~慢慢练吧~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值