opencv surf特征点匹配拼接源码
http://blog.csdn.net/huixingshao/article/details/42672073
/**
* @file SURF_Homography
* @brief SURF detector + descriptor + FLANN Matcher + FindHomography
* @author A. Huaman
*/ #include <stdio.h>
#include <iostream>
#include <cv.h>
#include "opencv2/core/core.hpp"
#include <opencv2/opencv.hpp>
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/nonfree/nonfree.hpp> using namespace cv;
using namespace std; #ifdef _DEBUG
#pragma comment (lib, "opencv_calib3d246d.lib")
#pragma comment (lib, "opencv_contrib246d.lib")
#pragma comment (lib,"opencv_imgproc246d.lib")
#pragma comment (lib, "opencv_core246d.lib")
#pragma comment (lib, "opencv_features2d246d.lib")
#pragma comment (lib, "opencv_flann246d.lib")
#pragma comment (lib, "opencv_gpu246d.lib")
#pragma comment (lib, "opencv_highgui246d.lib")
#pragma comment (lib, "opencv_legacy246d.lib")
#pragma comment (lib, "opencv_ml246d.lib")
#pragma comment (lib, "opencv_objdetect246d.lib")
#pragma comment (lib, "opencv_ts246d.lib")
#pragma comment (lib, "opencv_video246d.lib")
#pragma comment (lib, "opencv_nonfree246d.lib")
#else
#pragma comment (lib, "opencv_calib3d246.lib")
#pragma comment (lib, "opencv_contrib246.lib")
#pragma comment (lib, "opencv_imgproc246.lib")
#pragma comment (lib, "opencv_core246.lib")
#pragma comment (lib, "opencv_features2d246.lib")
#pragma comment (lib, "opencv_flann246.lib")
#pragma comment (lib, "opencv_gpu246.lib")
#pragma comment (lib, "opencv_highgui246.lib")
#pragma comment (lib, "opencv_legacy246.lib")
#pragma comment (lib, "opencv_ml246.lib")
#pragma comment (lib, "opencv_objdetect246.lib")
#pragma comment (lib, "opencv_ts246.lib")
#pragma comment (lib, "opencv_video246.lib")
#pragma comment (lib, "opencv_nonfree246.lib")
#endif int main()
{
initModule_nonfree();//初始化模块,使用SIFT或SURF时用到
Ptr<FeatureDetector> detector = FeatureDetector::create( "SURF" );//创建SIFT特征检测器,可改成SURF/ORB
Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create( "SURF" );//创建特征向量生成器,可改成SURF/ORB
Ptr<DescriptorMatcher> descriptor_matcher = DescriptorMatcher::create( "BruteForce" );//创建特征匹配器
if( detector.empty() || descriptor_extractor.empty() )
cout<<"fail to create detector!"; //读入图像
Mat img1 = imread("1.jpg");
Mat img2 = imread("2.jpg"); //特征点检测
double t = getTickCount();//当前滴答数
vector<KeyPoint> m_LeftKey,m_RightKey;
detector->detect( img1, m_LeftKey );//检测img1中的SIFT特征点,存储到m_LeftKey中
detector->detect( img2, m_RightKey );
cout<<"图像1特征点个数:"<<m_LeftKey.size()<<endl;
cout<<"图像2特征点个数:"<<m_RightKey.size()<<endl; //根据特征点计算特征描述子矩阵,即特征向量矩阵
Mat descriptors1,descriptors2;
descriptor_extractor->compute( img1, m_LeftKey, descriptors1 );
descriptor_extractor->compute( img2, m_RightKey, descriptors2 );
t = ((double)getTickCount() - t)/getTickFrequency();
cout<<"SIFT算法用时:"<<t<<"秒"<<endl; cout<<"图像1特征描述矩阵大小:"<<descriptors1.size()
<<",特征向量个数:"<<descriptors1.rows<<",维数:"<<descriptors1.cols<<endl;
cout<<"图像2特征描述矩阵大小:"<<descriptors2.size()
<<",特征向量个数:"<<descriptors2.rows<<",维数:"<<descriptors2.cols<<endl; //画出特征点
Mat img_m_LeftKey,img_m_RightKey;
drawKeypoints(img1,m_LeftKey,img_m_LeftKey,Scalar::all(-1),0);
drawKeypoints(img2,m_RightKey,img_m_RightKey,Scalar::all(-1),0);
//imshow("Src1",img_m_LeftKey);
//imshow("Src2",img_m_RightKey); //特征匹配
vector<DMatch> matches;//匹配结果
descriptor_matcher->match( descriptors1, descriptors2, matches );//匹配两个图像的特征矩阵
cout<<"Match个数:"<<matches.size()<<endl; //计算匹配结果中距离的最大和最小值
//距离是指两个特征向量间的欧式距离,表明两个特征的差异,值越小表明两个特征点越接近
double max_dist = 0;
double min_dist = 100;
for(int i=0; i<matches.size(); i++)
{
double dist = matches[i].distance;
if(dist < min_dist) min_dist = dist;
if(dist > max_dist) max_dist = dist;
}
cout<<"最大距离:"<<max_dist<<endl;
cout<<"最小距离:"<<min_dist<<endl; //筛选出较好的匹配点
vector<DMatch> goodMatches;
for(int i=0; i<matches.size(); i++)
{
if(matches[i].distance < 0.2 * max_dist)
{
goodMatches.push_back(matches[i]);
}
}
cout<<"goodMatch个数:"<<goodMatches.size()<<endl; //画出匹配结果
Mat img_matches;
//红色连接的是匹配的特征点对,绿色是未匹配的特征点
drawMatches(img1,m_LeftKey,img2,m_RightKey,goodMatches,img_matches,
Scalar::all(-1)/*CV_RGB(255,0,0)*/,CV_RGB(0,255,0),Mat(),2); imshow("MatchSIFT",img_matches);
IplImage result=img_matches; waitKey(0); //RANSAC匹配过程
vector<DMatch> m_Matches=goodMatches;
// 分配空间
int ptCount = (int)m_Matches.size();
Mat p1(ptCount, 2, CV_32F);
Mat p2(ptCount, 2, CV_32F); // 把Keypoint转换为Mat
Point2f pt;
for (int i=0; i<ptCount; i++)
{
pt = m_LeftKey[m_Matches[i].queryIdx].pt;
p1.at<float>(i, 0) = pt.x;
p1.at<float>(i, 1) = pt.y; pt = m_RightKey[m_Matches[i].trainIdx].pt;
p2.at<float>(i, 0) = pt.x;
p2.at<float>(i, 1) = pt.y;
} // 用RANSAC方法计算F
Mat m_Fundamental;
vector<uchar> m_RANSACStatus; // 这个变量用于存储RANSAC后每个点的状态
findFundamentalMat(p1, p2, m_RANSACStatus, FM_RANSAC); // 计算野点个数 int OutlinerCount = 0;
for (int i=0; i<ptCount; i++)
{
if (m_RANSACStatus[i] == 0) // 状态为0表示野点
{
OutlinerCount++;
}
}
int InlinerCount = ptCount - OutlinerCount; // 计算内点
cout<<"内点数为:"<<InlinerCount<<endl; // 这三个变量用于保存内点和匹配关系
vector<Point2f> m_LeftInlier;
vector<Point2f> m_RightInlier;
vector<DMatch> m_InlierMatches; m_InlierMatches.resize(InlinerCount);
m_LeftInlier.resize(InlinerCount);
m_RightInlier.resize(InlinerCount);
InlinerCount=0;
float inlier_minRx=img1.cols; //用于存储内点中右图最小横坐标,以便后续融合 for (int i=0; i<ptCount; i++)
{
if (m_RANSACStatus[i] != 0)
{
m_LeftInlier[InlinerCount].x = p1.at<float>(i, 0);
m_LeftInlier[InlinerCount].y = p1.at<float>(i, 1);
m_RightInlier[InlinerCount].x = p2.at<float>(i, 0);
m_RightInlier[InlinerCount].y = p2.at<float>(i, 1);
m_InlierMatches[InlinerCount].queryIdx = InlinerCount;
m_InlierMatches[InlinerCount].trainIdx = InlinerCount; if(m_RightInlier[InlinerCount].x<inlier_minRx) inlier_minRx=m_RightInlier[InlinerCount].x; //存储内点中右图最小横坐标 InlinerCount++;
}
} // 把内点转换为drawMatches可以使用的格式
vector<KeyPoint> key1(InlinerCount);
vector<KeyPoint> key2(InlinerCount);
KeyPoint::convert(m_LeftInlier, key1);
KeyPoint::convert(m_RightInlier, key2); // 显示计算F过后的内点匹配
Mat OutImage;
drawMatches(img1, key1, img2, key2, m_InlierMatches, OutImage);
cvNamedWindow( "Match features", 1);
cvShowImage("Match features", &IplImage(OutImage));
waitKey(0); cvDestroyAllWindows(); //矩阵H用以存储RANSAC得到的单应矩阵
Mat H = findHomography( m_LeftInlier, m_RightInlier, RANSAC ); //存储左图四角,及其变换到右图位置
std::vector<Point2f> obj_corners(4);
obj_corners[0] = Point(0,0); obj_corners[1] = Point( img1.cols, 0 );
obj_corners[2] = Point( img1.cols, img1.rows ); obj_corners[3] = Point( 0, img1.rows );
std::vector<Point2f> scene_corners(4);
perspectiveTransform( obj_corners, scene_corners, H); //画出变换后图像位置
Point2f offset( (float)img1.cols, 0);
line( OutImage, scene_corners[0]+offset, scene_corners[1]+offset, Scalar( 0, 255, 0), 4 );
line( OutImage, scene_corners[1]+offset, scene_corners[2]+offset, Scalar( 0, 255, 0), 4 );
line( OutImage, scene_corners[2]+offset, scene_corners[3]+offset, Scalar( 0, 255, 0), 4 );
line( OutImage, scene_corners[3]+offset, scene_corners[0]+offset, Scalar( 0, 255, 0), 4 );
imshow( "Good Matches & Object detection", OutImage ); waitKey(0);
imwrite("warp_position.jpg",OutImage); int drift = scene_corners[1].x; //储存偏移量 //新建一个矩阵存储配准后四角的位置
int width = int(max(abs(scene_corners[1].x), abs(scene_corners[2].x)));
int height= img1.rows; //或者:int height = int(max(abs(scene_corners[2].y), abs(scene_corners[3].y)));
float origin_x=0,origin_y=0;
if(scene_corners[0].x<0) {
if (scene_corners[3].x<0) origin_x+=min(scene_corners[0].x,scene_corners[3].x);
else origin_x+=scene_corners[0].x;}
width-=int(origin_x);
if(scene_corners[0].y<0) {
if (scene_corners[1].y) origin_y+=min(scene_corners[0].y,scene_corners[1].y);
else origin_y+=scene_corners[0].y;}
//可选:height-=int(origin_y);
Mat imageturn=Mat::zeros(width,height,img1.type()); //获取新的变换矩阵,使图像完整显示
for (int i=0;i<4;i++) {scene_corners[i].x -= origin_x; } //可选:scene_corners[i].y -= (float)origin_y; }
Mat H1=getPerspectiveTransform(obj_corners, scene_corners); //进行图像变换,显示效果
warpPerspective(img1,imageturn,H1,Size(width,height));
imshow("image_Perspective", imageturn);
waitKey(0); //图像融合
int width_ol=width-int(inlier_minRx-origin_x);
int start_x=int(inlier_minRx-origin_x);
cout<<"width: "<<width<<endl;
cout<<"img1.width: "<<img1.cols<<endl;
cout<<"start_x: "<<start_x<<endl;
cout<<"width_ol: "<<width_ol<<endl; uchar* ptr=imageturn.data;
double alpha=0, beta=1;
for (int row=0;row<height;row++) {
ptr=imageturn.data+row*imageturn.step+(start_x)*imageturn.elemSize();
for(int col=0;col<width_ol;col++)
{
uchar* ptr_c1=ptr+imageturn.elemSize1(); uchar* ptr_c2=ptr_c1+imageturn.elemSize1();
uchar* ptr2=img2.data+row*img2.step+(col+int(inlier_minRx))*img2.elemSize();
uchar* ptr2_c1=ptr2+img2.elemSize1(); uchar* ptr2_c2=ptr2_c1+img2.elemSize1(); alpha=double(col)/double(width_ol); beta=1-alpha; if (*ptr==0&&*ptr_c1==0&&*ptr_c2==0) {
*ptr=(*ptr2);
*ptr_c1=(*ptr2_c1);
*ptr_c2=(*ptr2_c2);
} *ptr=(*ptr)*beta+(*ptr2)*alpha;
*ptr_c1=(*ptr_c1)*beta+(*ptr2_c1)*alpha;
*ptr_c2=(*ptr_c2)*beta+(*ptr2_c2)*alpha; ptr+=imageturn.elemSize();
} } //imshow("image_overlap", imageturn);
//waitKey(0); Mat img_result=Mat::zeros(height,width+img2.cols-drift,img1.type());
uchar* ptr_r=imageturn.data; for (int row=0;row<height;row++) {
ptr_r=img_result.data+row*img_result.step; for(int col=0;col<imageturn.cols;col++)
{
uchar* ptr_rc1=ptr_r+imageturn.elemSize1(); uchar* ptr_rc2=ptr_rc1+imageturn.elemSize1(); uchar* ptr=imageturn.data+row*imageturn.step+col*imageturn.elemSize();
uchar* ptr_c1=ptr+imageturn.elemSize1(); uchar* ptr_c2=ptr_c1+imageturn.elemSize1(); *ptr_r=*ptr;
*ptr_rc1=*ptr_c1;
*ptr_rc2=*ptr_c2; ptr_r+=img_result.elemSize();
} ptr_r=img_result.data+row*img_result.step+imageturn.cols*img_result.elemSize();
for(int col=imageturn.cols;col<img_result.cols;col++)
{
uchar* ptr_rc1=ptr_r+imageturn.elemSize1(); uchar* ptr_rc2=ptr_rc1+imageturn.elemSize1(); uchar* ptr2=img2.data+row*img2.step+(col-imageturn.cols+drift)*img2.elemSize();
uchar* ptr2_c1=ptr2+img2.elemSize1(); uchar* ptr2_c2=ptr2_c1+img2.elemSize1(); *ptr_r=*ptr2;
*ptr_rc1=*ptr2_c1;
*ptr_rc2=*ptr2_c2; ptr_r+=img_result.elemSize();
}
} imshow("image_result", img_result);
//imwrite("final_result.jpg",img_result);
return 0;
}
opencv surf特征点匹配拼接源码的更多相关文章
- Android上掌纹识别第一步:基于OpenCV的6种肤色分割 源码和效果图
Android上掌纹识别第一步:基于OpenCV的6种肤色分割 源码和效果图 分类: OpenCV图像处理2013-02-21 21:35 6459人阅读 评论(8) 收藏 举报 原文链接 ht ...
- OpenCV——SURF特征检测、匹配与对象查找
SURF原理详解:https://wenku.baidu.com/view/2f1e4d8ef705cc1754270945.html SURF算法工作原理 选择图像中的POI(Points of i ...
- opencv::SURF特征
SURF特征基本介绍 SURF(Speeded Up Robust Features)特征关键特性: -特征检测 -尺度空间 -选择不变性 -特征向量 工作原理 . 选择图像中POI(Points o ...
- 手把手教你使用LabVIEW OpenCV dnn实现图像分类(含源码)
@ 目录 前言 一.什么是图像分类? 1.图像分类的概念 2.MobileNet简介 二.使用python实现图像分类(py_to_py_ssd_mobilenet.py) 1.获取预训练模型 2.使 ...
- 【OpenCV-ANN神经网络自动驾驶】树莓派OpenCV神经网络自动驾驶小车【源码+实物】
没错!这个是我的毕业设计!!! 整个电子信息学院唯一一个优秀毕业设计 拿到这里炫耀了 实物如下: 电脑端显示效果: 自动驾驶实现过程: 1. 收集图像数据.建立局域网,让主机和Raspberry Pi ...
- SURF算法与源码分析、下
上一篇文章 SURF算法与源码分析.上 中主要分析的是SURF特征点定位的算法原理与相关OpenCV中的源码分析,这篇文章接着上篇文章对已经定位到的SURF特征点进行特征描述.这一步至关重要,这是SU ...
- Opencv中使用Surf特征实现图像配准及对透视变换矩阵H的平移修正
图像配准需要将一张测试图片按照第二张基准图片的尺寸.角度等形态信息进行透视(仿射)变换匹配,本例通过Surf特征的定位和匹配实现图像配准. 配准流程: 1. 提取两幅图像的Surf特征 2. 对Sur ...
- 【浅墨著作】《OpenCV3编程入门》内容简单介绍&勘误&配套源码下载
经过近一年的沉淀和总结,<OpenCV3编程入门>一书最终和大家见面了. 近期有为数不少的小伙伴们发邮件给浅墨建议最好在博客里面贴出这本书的文件夹,方便大家更好的了解这本书的内容.事实上近 ...
- jQuery源码分析系列
声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 版本截止到2013.8.24 jQuery官方发布最新的的2.0.3为准 附上每一章的源码注释分析 :https://git ...
随机推荐
- 局域网如何通过SSH连接虚拟机装的centOS系统
首先,在一个局域网内的一台机器上装了虚拟机,虚拟机上装了centos系统: 但是,只有本机能连接centos,其他电脑都连不上: ping了一下发现不通,然后排查原因. 我发现局域网内的机器IP都是: ...
- javascript fetch 跨域请求时 session失效问题
javascript 使用fetch进行跨域请求时默认是不带cookie的,所以会造成 session失效. fetch(url, { method: 'POST', credentials: 'in ...
- machine learning for hacker记录(4) 智能邮箱(排序学习&推荐系统)
本章是上一章邮件过滤技术的延伸,上一章的内容主要是过滤掉垃圾邮件,而这里要讲的是对那些正常的邮件是否可以加入个性化元素,由于每个用户关心的主题并非一样(有人喜欢技术类型的邮件或者购物促销方便的内容邮件 ...
- IC卡、ID卡、M1卡、射频卡的区别是什么
IC卡.ID卡.M1卡.射频卡都是我们常见的一种智能卡,但是很多的顾客还是不清楚IC卡.ID卡.M1卡.射频卡的区别是什么,下面我们一起来看看吧. 所谓的IC卡就是集成电路卡,是继磁卡之后出现的又一种 ...
- Ubuntu下安装Python3.4及用python编译py文件
1.安装python 3.4程序 sudo apt-get install python3.4 2.python 3.4是被默认安装在/usr/local/lib/python3.4,删除默认pyth ...
- POJ2559 Largest Rectangle in a Histogram —— 单调栈
题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Lim ...
- Codeforces Round #258 (Div. 2) D. Count Good Substrings —— 组合数学
题目链接:http://codeforces.com/problemset/problem/451/D D. Count Good Substrings time limit per test 2 s ...
- codeforces A. Fox and Box Accumulation 解题报告
题目链接:http://codeforces.com/problemset/problem/388/A 题目意思:有 n 个 boxes,每个box 有相同的 size 和 weight,但是stre ...
- 安装python解释器
Python目前已支持所有主流操作系统,在Linux,Unix,Mac系统上自带Python环境,在Windows系统上需要安装一下,超简单 打开官网 https://www.python.org/d ...
- TCP连接过程
TCP建立连接与释放连接 最近复习准备<计算机网络>考试,感觉TCP协议建立连接与释放连接这两个过程比较重要,所以把自己理解的部分写下来. 1.建立连接:(三次握手) (1)客户端发 ...