运行环境: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的更多相关文章

  1. VS2010+Opencv+SIFT以及出现的问题-关于代码sift_3_c的说明

    http://blog.sina.com.cn/s/blog_a6b913e30101dvrt.html 一.前提 安装Opencv,因该版本的SIFT是基于Opencv的. 下载SIFT源码,见Ro ...

  2. RPi 2B python opencv camera demo example

    /************************************************************************************** * RPi 2B pyt ...

  3. Opencv Sift算子特征提取与匹配

    SIFT算法的过程实质是在不同尺度空间上查找特征点(关键点),用128维方向向量的方式对特征点进行描述,最后通过对比描述向量实现目标匹配. 概括起来主要有三大步骤: 1.提取关键点: 2.对关键点附加 ...

  4. OpenCV SIFT原理与源码分析

    http://blog.csdn.net/xiaowei_cqu/article/details/8069548 SIFT简介 Scale Invariant Feature Transform,尺度 ...

  5. python opencv SIFT,获取特征点的坐标位置

    备注:SIFT算法的实质是在不同的尺度空间上查找关键点(特征点),并计算出关键点的方向.SIFT所查找到的关键点是一些十分突出,不会因光照,仿射变换和噪音等因素而变化的点,如角点.边缘点.暗区的亮点及 ...

  6. OpenCV——SIFT特征检测与匹配

    SIFT特征和SURF特征比较 比较项目 SIFT SURF 尺度空间极值检测 使用高斯滤波器,根据不同尺度的高斯差(DOG)图像寻找局部极值 使用方形滤波器,利用海森矩阵的行列式值检测极值,并利用积 ...

  7. Opencv Sift和Surf特征实现图像无缝拼接生成全景图像

    Sift和Surf算法实现两幅图像拼接的过程是一样的,主要分为4大部分: 1. 特征点提取和描述 2. 特征点配对,找到两幅图像中匹配点的位置 3. 通过配对点,生成变换矩阵,并对图像1应用变换矩阵生 ...

  8. opencv::sift特征提取

    SIFT特征检测介绍 SIFT(Scale-Invariant Feature Transform)特征检测关键特性: -建立尺度空间,寻找极值 -关键点定位(寻找关键点准确位置与删除弱边缘) -关键 ...

  9. python+opencv+sift环境配置教程

    最近在做对应点估计homography,需要用到opencv,c++的接口不如python的接口来的方便 但是在安装python接口的opencv的时候,遇到了各种问题,主要是函数找不到的问题 比如在 ...

随机推荐

  1. 007_FreeROTS队列

    (一)目的:进程间的通信 (二)队列创建 1. 动态创建队列,函数 xQueueCreate() QueueHandle_t xQueueCreate( UBaseType_t uxQueueLeng ...

  2. ckeditor不能粘贴word的问题

    在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper. 通过知乎提供的思路找到粘贴的原理,通过TheViper找 ...

  3. learning express step(十二)

    learning express view engine function const express = require('express'); const app = express(); app ...

  4. Linux下的电路设计辅助软件

    造冰箱的大熊猫@cnblogs 2019/8/21 最近调研了以下开源的电路设计辅助软件(EDA),汇总如下 1.gEDA 官网:geda-project.org 老牌的开源EDA软件,诞生于上世纪9 ...

  5. seq2seq聊天模型(三)—— attention 模型

    注意力seq2seq模型 大部分的seq2seq模型,对所有的输入,一视同仁,同等处理. 但实际上,输出是由输入的各个重点部分产生的. 比如: (举例使用,实际比重不是这样) 对于输出"晚上 ...

  6. numpy基础一

    常用,常忘 1.随机矩阵 np.random.rand(4,3) array([[ 0.06679473, 0.71073515, 0.5694172 ], [ 0.95018143, 0.60161 ...

  7. 1626:【例 2】Hankson 的趣味题

    1626:[例 2]Hankson 的趣味题题解 [题目描述] Hanks 博士是 BT(Bio-Tech,生物技术)领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson ...

  8. python爬虫demo01

    python爬虫demo01 1 import requests, json, time, sys 2 from bs4 import BeautifulSoup 3 from contextlib ...

  9. 在树莓派4b上安装 ROS MELODIC 源码安装

    按照以下步骤照做就可以了,很简单的,就是浪费一点点时间罢了.也可以退而求其次,买个树莓派3B+来玩,哈哈. Step 1: Install Dependecies and Download the P ...

  10. idea 2018注册码(激活码)

    最近做一个项目,用idea 社区版的   但是缺少了好多功能 无奈只能用专业版的,但是需要注册激活  下面是我的注册方法 1.打开了idea  会提示让激活  选择Licensse server 2. ...