基于opencv的特征点匹配法

基于opencv的特征点匹配法

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

#include 
#include 
#include 
#include 
#include 
#include 
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<
//相机畸变参数
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 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);
// 计算匹配点数

BruteForceMatcherfloat> >matcher;

vector 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:第一次写博客,格式还比较烂~慢慢练吧~

你可能感兴趣的:(opencv)