【CS】尺度不变特征变换匹配算法SIFT(2)

尺度不变特征变换匹配算法SIFT(2)

e-mail:[email protected]

SIFT算法

在10月初,草草学习了一下SIFT(可以戳这里查看),主要是调用opencv函数库了的函数进行了实践,而并没有深入了解SIFT描述子的原理以及opencv中相关函数的用法和参数说明。本篇blog作为LZ的小笔记,记录一下opencv中相关函数的说明,对于SIFT特征的原理后续将花时间继续了解。

C++代码

环境:vs2010+opencv2.3.1+win7
×64

这部分代码还是使用上一篇SIFT的代码,本篇重在了解一些函数和数据结构。

#include <opencv2/opencv.hpp>
#include <istream>  

using namespace std;
using namespace cv;
int main()
{
    //read the two input images
    Mat image1 = imread("image1.jpg");
    Mat image2 = imread("image2.jpg");
    //if failed
    if(image1.empty()||image2.empty())
    {
        cout<<"error,the image is not exist"<<endl;
        return -1;
    }
    //difine a sift detector
    SiftFeatureDetector siftDetector;
    //store key points
    vector<KeyPoint> keypoint1,keypoint2;
    //detect image with SIFT,get key points
    siftDetector.detect(image1,keypoint1);
    Mat outImage1;
    //draw key points at the out image and show to the user
    drawKeypoints(image1,keypoint1,outImage1,Scalar(255,0,0));  

    imshow("original_image1",image1);
    imshow("sift_image1",outImage1);  

    Mat outImage2;  

    siftDetector.detect(image2,keypoint2);
    drawKeypoints(image2,keypoint2,outImage2,Scalar(255,0,0));  

    imshow("sift_image2.jpg",outImage2);
    //imwrite("sift_result2.jpg",outImage2);
    //store 10 keypoints in order to watch the effect clearly
    vector<KeyPoint> keypoint3,keypoint4;
    for(int i=0;i<10;i++)
    {
        keypoint3.push_back(keypoint1[i]);
        keypoint4.push_back(keypoint2[i]);
    }
    // difine a sift descriptor extractor
    SiftDescriptorExtractor extractor;
    //store the descriptor of each image
    Mat descriptor1,descriptor2;
    BruteForceMatcher<L2<float>> matcher;  

    vector<DMatch> matches;
    Mat img_matches;
    //compute the descriptor of each image
    extractor.compute(image1,keypoint3,descriptor1);
    extractor.compute(image2,keypoint4,descriptor2);
    //match
    matcher.match(descriptor1,descriptor2,matches);
    //show the result
    drawMatches(image1,keypoint3,image2,keypoint4,matches,img_matches,Scalar(255,0,0));
    imshow("matches",img_matches);
    //store the match_image
    //imwrite("matches.jpg",img_matches);  

    waitKey(0);
    return 0;
}  

opencv相关函数和数据结构说明

1.drawMatcher():Draws the found matches of keypoints from two images.

参考:http://docs.opencv.org/2.4/modules/features2d/doc/drawing_function_of_keypoints_and_matches.html

C++: void drawMatches(const Mat& img1,
const vector<KeyPoint>& keypoints1, const Mat& img2, const  vector<KeyPoint>& keypoints2,
const vector<vector<DMatch>>& matches1to2
  Mat& outImg, const Scalar& matchColor=Scalar::all(-1),  const
Scalar& singlePointColor=Scalar::all(-1), 
 const vector<vector<char>>& matchesMask=vector<vector<char>
>(), int flags=DrawMatchesFlags::DEFAULT )

  • img1 – First source image.
  • keypoints1 – Keypoints from the first source image.
  • img2 – Second source image.
  • keypoints2 – Keypoints from the second source image.
  • matches1to2 – Matches from the first image to the second one, which means that keypoints1[i] has
    a corresponding point in keypoints2[matches[i]] .
  • outImg – Output image. Its content depends on the flags value defining what is drawn in the
    output image. See possible flags bit values below.
  • matchColor – Color of matches (lines and connected keypoints). If matchColor==Scalar::all(-1) ,
    the color is generated randomly.
  • singlePointColor – Color of single keypoints (circles), which means that keypoints do not have the matches. If singlePointColor==Scalar::all(-1) ,
    the color is generated randomly.
  • matchesMask – Mask determining which matches are drawn. If the mask is empty, all matches are drawn.
  • flags – Flags setting drawing features. Possible flags bit
    values are defined by DrawMatchesFlags.

2.DMatch:Class for matching keypoint descriptors: query descriptor index, train descriptor index, train image index, and distance between descriptors.

可参考:http://docs.opencv.org/master/d4/de0/classcv_1_1DMatch.html

<span style="font-family:Microsoft YaHei;">       struct DMatch
       {
              //三个构造函数
           DMatch(): queryIdx(-1), trainIdx(-1),imgIdx(-1),distance(std::numeric_limits<float>::max()) {}
           DMatch(int  _queryIdx, int  _trainIdx, float  _distance ) :
                            queryIdx( _queryIdx),trainIdx( _trainIdx), imgIdx(-1),distance( _distance) {}
           DMatch(int  _queryIdx, int  _trainIdx, int  _imgIdx, float  _distance ) :
                   queryIdx(_queryIdx), trainIdx( _trainIdx), imgIdx( _imgIdx),distance( _distance) {}

           intqueryIdx;  //此匹配对应的查询图像的特征描述子索引
           inttrainIdx;   //此匹配对应的训练(模板)图像的特征描述子索引
           intimgIdx;    //训练图像的索引(若有多个)
           float distance;  //两个特征向量之间的欧氏距离,越小表明匹配度越高。
           booloperator < (const DMatch &m) const;
       };</span>

一般使用Brute-force descriptor matcher进行匹配,结果并不具有可读性(戳这里看图),那么这里请留意匹配的结果保存在了vector<DMatch>定义的动态数组matches中,这就意味着我们可以对匹配结果进行一系列操作,比如再drawMatches()函数前添加一句:matches.erase(matches.begin()+25,matches.end());
既可以选择最新的25个匹配结果。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-11 20:04:33

【CS】尺度不变特征变换匹配算法SIFT(2)的相关文章

特征提取之SIFT(尺度不变性特征变换)

SIFT(Scale-invariant feature transform,尺度不变性特征变换)是一种检测局部特征的算法,该算法通过求一幅图中的特征点(interest points,or corner points)及其有关scale和orientation的描述子得到特征并进行图像特征点匹配,获得了良好效果,详细解析如下: 算法描述 整个算法分为以下几个部分: 1.构建尺度空间 尺度空间理论目的是模拟图像数据的多尺度特性,高斯卷积核是实现尺度变换的唯一卷积核,于是一副二维图像的尺度空间定义

[翻译]鲁棒的尺度不变特征匹配在遥感图像配准中应用(Robust Scale-Invariant Feature Matching for Remote Sensing Image Registration)

李乔亮,汪国有,刘建国,会员,IEEE,和陈少波 2008年8月7日接收;2008年10月22日和2008年11月27日修改.2009年2月2日首版:当前版本出版于2009年4月17日.本项工作由中国国家基础研究项目60672060资助. 中国湖北省武汉市华中科技大学模式识别与人工智能国家重点实验室,邮编430074(邮箱:[email protected];   [email protected];  [email protected];  [email protected]) 数字对象识别编

【学习笔记】SIFT尺度不变特征 (配合UCF-CRCV课程视频)

SIFT尺度不变特征 D. Lowe. Distinctive image features from scale-invariant key points, IJCV 2004 -Lecture 05 - Scale-invariant Feature Transform (SIFT) - https://www.youtube.com/watch?v=NPcMS49V5hg 本文是上面UCF-CRCV课程视频的学习笔记. DOG(Difference of Gaussian)角点 / Har

【特征匹配】SIFT原理与C源码剖析

关于SIFT的原理已经有很多大牛的博客上做了解析,本文重点将以Rob Hess等人用C实现的代码做解析,结合代码SIFT原理会更容易理解.一些难理解点的用了☆标注. 欢迎大家批评指正! SIFT(Scale-invariant feature transform)即尺度不变特征转换,提取的局部特征点具有尺度不变性,且对于旋转,亮度,噪声等有很高的稳定性. SIFT特征点提取 本文将以下函数为参照顺序介绍SIFT特征点提取与描述方法. 1.图像预处理 2.构建高斯金字塔(不同尺度下的图像) 3.生

机器学习技法:Homework #5 特征变换&amp;Soft-Margin SVM相关习题

原文地址:https://www.jianshu.com/p/6bf801bdc644 特征变换 问题描述 程序实现 # coding: utf-8 import numpy as np from cvxopt import matrix, solvers from sklearn import svm def gen_data(): X = [[1, 0], [0, 1], [0, -1], [-1, 0], [0, 2], [0, -2], [-2, 0]] X = np.array(X)

模式匹配之常见匹配算法---SIFT/SURF、haar特征、广义hough变换的特性对比分析

识别算法概述: SIFT/SURF基于灰度图, 一.首先建立图像金字塔,形成三维的图像空间,通过Hessian矩阵获取每一层的局部极大值,然后进行在极值点周围26个点进行NMS,从而得到粗略的特征点,再使用二次插值法得到精确特征点所在的层(尺度),即完成了尺度不变. 二.在特征点选取一个与尺度相应的邻域,求出主方向,其中SIFT采用在一个正方形邻域内统计所有点的梯度方向,找到占80%以上的方向作为主方向:而SURF则选择圆形邻域,并且使用活动扇形的方法求出特征点主方向,以主方向对齐即完成旋转不变

【特征匹配】SIFT原理之KD树+BBF算法解析

继上一篇中已经介绍了SIFT原理点击打开链接,最后得到了一系列特征点,每个特征点对应一个128维向量.假如现在有两副图片都已经提取到特征点,现在要做的就是匹配上相似的特征点. 相似性查询有两种基本方式:1.范围查询:即给点查询点和查询阈值,从数据集中找出所有与查询点距离小于阈值的点. 2.K近邻查询:给点查询点及正整数K,从数据集中找到与查询点最近的K个数据,当K=1时,就是最近邻查询. 特征匹配算子可以分为两类:1.穷举法:即将数据集中的点与查询点逐一计算距离,如果图1提取到N1个特征点,图2

MLlib特征变换方法

Spark1.6.2.2.3 PCA 算法介绍: 主成分分析是一种统计学方法,它使用正交转换从一系列可能相关的变量中提取线性无关变量集,提取出的变量集中的元素称为主成分.使用PCA方法可以对变量集合进行降维.下面的示例将会展示如何将5维特征向量转换为3维主成分向量. scala代码 import org.apache.spark.ml.feature.PCA import org.apache.spark.ml.linalg.Vectors val data = Array( Vectors.s

二十种特征变换方法及Spark MLlib调用实例(Scala/Java/python)(二)

VectorIndexer 算法介绍: VectorIndexer解决数据集中的类别特征Vector.它可以自动识别哪些特征是类别型的,并且将原始值转换为类别指标.它的处理流程如下: 1.获得一个向量类型的输入以及maxCategories参数. 2.基于原始数值识别哪些特征需要被类别化,其中最多maxCategories需要被类别化. 3.对于每一个类别特征计算0-based类别指标. 4.对类别特征进行索引然后将原始值转换为指标. 索引后的类别特征可以帮助决策树等算法处理类别型特征,并得到较