[OpenCV] sift demo
运行环境:vs2012+opencv320
sift 需要的头文件为 <opencv2/xfeatures2d.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp> using namespace cv;
using namespace std; bool refineMatchesWithHomography(
const std::vector<cv::KeyPoint>& queryKeypoints,
const std::vector<cv::KeyPoint>& trainKeypoints,
float reprojectionThreshold,
std::vector<cv::DMatch>& matches,
cv::Mat& homography)
{
const int minNumberMatchesAllowed = 8; if (matches.size() < minNumberMatchesAllowed)
return false; // Prepare data for cv::findHomography
std::vector<cv::Point2f> srcPoints(matches.size());
std::vector<cv::Point2f> dstPoints(matches.size()); for (size_t i = 0; i < matches.size(); i++) {
srcPoints[i] = trainKeypoints[matches[i].trainIdx].pt;
dstPoints[i] = queryKeypoints[matches[i].queryIdx].pt;
} // Find homography matrix and get inliers mask
std::vector<unsigned char> inliersMask(srcPoints.size());
homography = cv::findHomography(srcPoints, dstPoints, CV_FM_RANSAC,reprojectionThreshold, inliersMask); std::vector<cv::DMatch> inliers;
for (size_t i = 0; i < inliersMask.size(); i++) {
if (inliersMask[i])
inliers.push_back(matches[i]);
} matches.swap(inliers);
return matches.size() > minNumberMatchesAllowed;
} bool comp(vector<DMatch>& a,vector<DMatch>& b)
{
return a[0].distance/a[1].distance < b[0].distance/b[1].distance;
} void main()
{
Ptr<xfeatures2d::SIFT>feature=xfeatures2d::SIFT::create(); Mat input1 = imread("sift_img\\16.png",1);
Mat input2 = imread("sift_img\\11.png",1); vector<KeyPoint>kp1,kp2;
Mat des1,des2;
Mat output1,output2; feature->detectAndCompute(input1,cv::noArray(),kp1,des1);
drawKeypoints(input1,kp1,output1); feature->detectAndCompute(input2,cv::noArray(),kp2,des2);
drawKeypoints(input2,kp2,output2); vector<DMatch>matches;
vector<vector<DMatch> >Dmatches;
Ptr<cv::DescriptorMatcher> matcher_knn = new BFMatcher();
Ptr<cv::DescriptorMatcher> matcher = new BFMatcher(NORM_L2,true);
matcher->match(des1,des2,matches); matcher_knn->knnMatch(des1,des2,Dmatches,2);
sort(Dmatches.begin(),Dmatches.end(),comp); vector<DMatch> good;
for(int i=0;i<Dmatches.size();i++){
if(Dmatches[i][0].distance < 0.75*Dmatches[i][1].distance)
good.push_back(Dmatches[i][0]);
} Mat imResultOri;
drawMatches(output1, kp1, output2, kp2, matches, imResultOri,CV_RGB(0,255,0), CV_RGB(0,255,0)); Mat matHomo;
refineMatchesWithHomography(kp1, kp2, 3, matches, matHomo);
cout << "[Info] Homography T : " << endl << matHomo << endl; Mat imResult;
drawMatches(output1, kp1, output2, kp2, matches, imResult,CV_RGB(0,255,0), CV_RGB(0,255,0)); Mat Mgood;
drawMatches(output1, kp1, output2, kp2, good, Mgood,CV_RGB(0,255,0), CV_RGB(0,255,0)); imshow("ransc",imResult);
imshow("knn_match",Mgood);
waitKey(0); return;
}

[OpenCV] sift demo的更多相关文章
- VS2010+Opencv+SIFT以及出现的问题-关于代码sift_3_c的说明
http://blog.sina.com.cn/s/blog_a6b913e30101dvrt.html 一.前提 安装Opencv,因该版本的SIFT是基于Opencv的. 下载SIFT源码,见Ro ...
- RPi 2B python opencv camera demo example
/************************************************************************************** * RPi 2B pyt ...
- Opencv Sift算子特征提取与匹配
SIFT算法的过程实质是在不同尺度空间上查找特征点(关键点),用128维方向向量的方式对特征点进行描述,最后通过对比描述向量实现目标匹配. 概括起来主要有三大步骤: 1.提取关键点: 2.对关键点附加 ...
- OpenCV SIFT原理与源码分析
http://blog.csdn.net/xiaowei_cqu/article/details/8069548 SIFT简介 Scale Invariant Feature Transform,尺度 ...
- python opencv SIFT,获取特征点的坐标位置
备注:SIFT算法的实质是在不同的尺度空间上查找关键点(特征点),并计算出关键点的方向.SIFT所查找到的关键点是一些十分突出,不会因光照,仿射变换和噪音等因素而变化的点,如角点.边缘点.暗区的亮点及 ...
- OpenCV——SIFT特征检测与匹配
SIFT特征和SURF特征比较 比较项目 SIFT SURF 尺度空间极值检测 使用高斯滤波器,根据不同尺度的高斯差(DOG)图像寻找局部极值 使用方形滤波器,利用海森矩阵的行列式值检测极值,并利用积 ...
- Opencv Sift和Surf特征实现图像无缝拼接生成全景图像
Sift和Surf算法实现两幅图像拼接的过程是一样的,主要分为4大部分: 1. 特征点提取和描述 2. 特征点配对,找到两幅图像中匹配点的位置 3. 通过配对点,生成变换矩阵,并对图像1应用变换矩阵生 ...
- opencv::sift特征提取
SIFT特征检测介绍 SIFT(Scale-Invariant Feature Transform)特征检测关键特性: -建立尺度空间,寻找极值 -关键点定位(寻找关键点准确位置与删除弱边缘) -关键 ...
- python+opencv+sift环境配置教程
最近在做对应点估计homography,需要用到opencv,c++的接口不如python的接口来的方便 但是在安装python接口的opencv的时候,遇到了各种问题,主要是函数找不到的问题 比如在 ...
随机推荐
- [Luogu] 稳定婚姻
https://www.luogu.org/problemnew/show/1407 tarjan求一下强连通分量,然后判断一下两个人是否在同一强连通分量中 #include<iostream& ...
- CF915E 动态开线段树
CF915E 动态开线段树 题面 因为\(n\le10^9\),所以动态开点,线段树维护\([1,n]\)天非工作日数量. 之前的结构体写法被卡了,只能改成函数传l,r(虽然也不难) 动态开点好写,但 ...
- Driver对 (一对两对的对):specific/mini VS general
老是听说miniport,port,在这里算是搞清楚了.mini就是specific(特殊)的意思.在微软的驱动层次里面,最底层的一般都是比较特殊的,但是为了满足系统的可拓展.可维护.通用等要求,微软 ...
- LOJ3120. 「CTS2019」珍珠 [容斥,生成函数]
传送门 思路 非常显然,就是要统计有多少种方式使得奇数的个数不超过\(n-2m\).(考场上这个都没想到真是身败名裂了--) 考虑直接减去钦点\(n-2m+1\)个奇数之后的方案数,但显然这样会算重, ...
- mac charles 代理https
1.安装根证书:help - ssl proxying - install charles root certificate 2.这时候会弹出一个根证书界面,如果没有弹出,则可以去chrome,高级设 ...
- 提交项目到Github
create a new repository on the command line git init git add README.md git commit -m "first com ...
- redis之redis-cluster配置
为什么要用redis-cluster 并发问题 redis官方生成可以达到 10万/每秒,每秒执行10万条命令 假如业务需要每秒100万的命令执行呢? 数据量太大 一台服务器内存正常是16~256G, ...
- msf端口扫描
使用MSF发现主机和端口扫描 使用search命令查找需要的模块 MSF模块太多,记不住怎么办!!! 我们不需要记住所有模块,我们只要能找到我们想用的模块就行,平时积累使用的模块也行哦! 比如,我们通 ...
- 5.3.3 自定义writable和RawComparatorWritable
5.3.3 自定义writable (1)构造员工writable Hadoop虽然已经实现了一些非常有用的Writable,而且你可以使用他们的组合做很多事情,但是如果你想构造一些更加复杂的结果,你 ...
- OpenFOAM 中的边界条件(一)【转载】
链接:http://xiaopingqiu.github.io/2016/04/02/Boundary-conditions-in-OpenFOAM1/ 本系列解读 OpenFOAM 中边界条件的实现 ...