• 大小: 10KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2021-06-14
  • 语言: C/C++
  • 标签: sift  surf  ransac  c++  opencv  

资源简介

在windows上利用OpenCV和vs2010实现了sift和surf粗配准,利用Ransac实现精确配准,C++源码,可以运行。

资源截图

代码片段和文件信息

#include 
#include 
#include   
#include   
#include 
#include 
#include   
#include   

using namespace cv; 
using namespace std; 

int main()
{
initModule_nonfree();//初始化模块,使用SIFT或SURF时用到 
Ptr detector = FeatureDetector::create( “SURF“ );//创建SIFT特征检测器,可改成SURF/ORB
PtrriptorExtractor> descriptor_extractor = DescriptorExtractor::create( “SURF“ );//创建特征向量生成器,可改成SURF/ORB
PtrriptorMatcher> descriptor_matcher = DescriptorMatcher::create( “BruteForce“ );//创建特征匹配器  
if( detector.empty() || descriptor_extractor.empty() )  
cout<<“fail to create detector!“;  

//读入图像  
Mat img1 = imread(“C://Users//YYL//Desktop//c_FourCamera_Match//SIFT_RANSAC//TestImage//Pair2//1.JPG“);  
Mat img2 = imread(“C://Users//YYL//Desktop//c_FourCamera_Match//SIFT_RANSAC//TestImage//Pair2//2.JPG“);  

//特征点检测  
double t = getTickCount();//当前滴答数  
vector m_LeftKeym_RightKey;  
detector->detect( img1 m_LeftKey );//检测img1中的SIFT特征点,存储到m_LeftKey中  
detector->detect( img2 m_RightKey );  
cout<<“图像1特征点个数:“< cout<<“图像2特征点个数:“<
//根据特征点计算特征描述子矩阵,即特征向量矩阵  
Mat descriptors1descriptors2;  
descriptor_extractor->compute( img1 m_LeftKey descriptors1 );  
descriptor_extractor->compute( img2 m_RightKey descriptors2 );  
t = ((double)getTickCount() - t)/getTickFrequency();  
cout<<“SIFT算法用时:“<
cout<<“图像1特征描述矩阵大小:“<riptors1.size()  
<<“,特征向量个数:“<riptors1.rows<<“,维数:“<riptors1.cols< cout<<“图像2特征描述矩阵大小:“<riptors2.size()  
<<“,特征向量个数:“<riptors2.rows<<“,维数:“<riptors2.cols<
//画出特征点  
Mat img_m_LeftKeyimg_m_RightKey;  
drawKeypoints(img1m_LeftKeyimg_m_LeftKeyScalar::all(-1)0);  
drawKeypoints(img2m_RightKeyimg_m_RightKeyScalar::all(-1)0);  
//imshow(“Src1“img_m_LeftKey);  
//imshow(“Src2“img_m_RightKey);  

//特征匹配  
vector matches;//匹配结果  
descriptor_matcher->match( descriptors1 descriptors2 matches );//匹配两个图像的特征矩阵  
cout<<“Match个数:“<
//计算匹配结果中距离的最大和最小值  
//距离是指两个特征向量间的欧式距离,表明两个特征的差异,值越小表明两个特征点越接近  
double max_dist = 0;  
double min_dist = 100;  
for(int i=0; i {  
double dist = matches[i].distance;  
if(dist < min_dist) min_dist = dist;  
if(dist > max_dist) max_dist = dist;  
}  
cout<<“最大距离:“< cout<<“最小距离:“<
//筛选出较好的匹配点  
vector goodMatches;  
for(int i=0; i {  
if(matches[i].distance < 0.2 * max_dist)  
{  
goodMatches.push_back(matches[i]);  
}  
}  
cout<<“goodMatch个数:“<
//画出匹配结果  
Mat img_matches;  
//红色连接的是匹配的特征点对,绿色是未匹配的特征点  
drawMatches(img1m_LeftKeyimg2m_RightKeygoodMa

评论

共有 条评论