http://blog.csdn.net/xiaowei_cqu/article/details/8069548

SIFT简介

Scale Invariant Feature Transform,尺度不变特征变换匹配算法,是由David G. Lowe在1999年(《Object Recognition from Local Scale-Invariant Features》)提出的高效区域检测算法,在2004年(《Distinctive Image Features from Scale-Invariant Keypoints》)得以完善。

SIFT特征对旋转、尺度缩放、亮度变化等保持不变性,是非常稳定的局部特征,现在应用很广泛。而SIFT算法是将Blob检测,特征矢量生成,特征匹配搜索等步骤结合在一起优化。我会更新一系列文章,分析SIFT算法原理及OpenCV 2.4.2实现的SIFT源码:

  1. DoG尺度空间构造(Scale-space extrema detection
  2. 关键点搜索与定位(Keypoint localization
  3. 方向赋值(Orientation assignment
  4. 关键点描述(Keypoint descriptor
  5. OpenCV实现:特征检测器FeatureDetector
  6. SIFT中LoG和DoG的比较
OpenCV2.3之后实现了SIFT的代码,2.4改掉了一些bug。本系列文章主要分析OpenCV 2.4.2SIFT函数源码。
SIFT位于OpenCV nonfree的模块,David G. Lowe申请了算法的版权,请尊重作者权力,务必在允许范围内使用。
 

SIFT in OpenCV

OpenCV中的SIFT函数主要有两个接口。

构造函数:

  1. SIFT::SIFT(int nfeatures=0, int nOctaveLayers=3, double contrastThreshold=0.04, double edgeThreshold=
  2. 10, double sigma=1.6)

nfeatures:特征点数目(算法对检测出的特征点排名,返回最好的nfeatures个特征点)。
nOctaveLayers:金字塔中每组的层数(算法中会自己计算这个值,后面会介绍)。
contrastThreshold:过滤掉较差的特征点的对阈值。contrastThreshold越大,返回的特征点越少。
edgeThreshold:过滤掉边缘效应的阈值。edgeThreshold越大,特征点越多(被多滤掉的越少)。
sigma:金字塔第0层图像高斯滤波系数,也就是σ。

 

重载操作符:

  1. void SIFT::operator()(InputArray img, InputArray mask, vector<KeyPoint>& keypoints, OutputArray
  2. descriptors, bool useProvidedKeypoints=false)

img:8bit灰度图像
mask:图像检测区域(可选)
keypoints:特征向量矩阵
descipotors:特征点描述的输出向量(如果不需要输出,需要传cv::noArray())。
useProvidedKeypoints:是否进行特征点检测。ture,则检测特征点;false,只计算图像特征描述。

 

函数源码

构造函数SIFT()主要用来初始化参数,并没有特定的操作:
  1. SIFT::SIFT( int _nfeatures, int _nOctaveLayers,
  2. double _contrastThreshold, double _edgeThreshold, double _sigma )
  3. : nfeatures(_nfeatures), nOctaveLayers(_nOctaveLayers),
  4. contrastThreshold(_contrastThreshold), edgeThreshold(_edgeThreshold), sigma(_sigma)
  5. // sigma:对第0层进行高斯模糊的尺度空间因子。
  6. // 默认为1.6(如果是软镜摄像头捕获的图像,可以适当减小此值)
  7. {
  8. }

主要操作还是利用重载操作符()来执行:

  1. void SIFT::operator()(InputArray _image, InputArray _mask,
  2. vector<KeyPoint>& keypoints,
  3. OutputArray _descriptors,
  4. bool useProvidedKeypoints) const
  5. // mask :Optional input mask that marks the regions where we should detect features.
  6. // Boolean flag. If it is true, the keypoint detector is not run. Instead,
  7. // the provided vector of keypoints is used and the algorithm just computes their descriptors.
  8. // descriptors – The output matrix of descriptors.
  9. // Pass cv::noArray() if you do not need them.
  10. {
  11. Mat image = _image.getMat(), mask = _mask.getMat();
  12. if( image.empty() || image.depth() != CV_8U )
  13. CV_Error( CV_StsBadArg, "image is empty or has incorrect depth (!=CV_8U)" );
  14. if( !mask.empty() && mask.type() != CV_8UC1 )
  15. CV_Error( CV_StsBadArg, "mask has incorrect type (!=CV_8UC1)" );
  16. // 得到第1组(Octave)图像
  17. Mat base = createInitialImage(image, false, (float)sigma);
  18. vector<Mat> gpyr, dogpyr;
  19. // 每层金字塔图像的组数(Octave)
  20. int nOctaves = cvRound(log( (double)std::min( base.cols, base.rows ) ) / log(2.) - 2);
  21. // double t, tf = getTickFrequency();
  22. // t = (double)getTickCount();
  23. // 构建金字塔(金字塔层数和组数相等)
  24. buildGaussianPyramid(base, gpyr, nOctaves);
  25. // 构建高斯差分金字塔
  26. buildDoGPyramid(gpyr, dogpyr);
  27. //t = (double)getTickCount() - t;
  28. //printf("pyramid construction time: %g\n", t*1000./tf);
  29. // useProvidedKeypoints默认为false
  30. // 使用keypoints并计算特征点的描述符
  31. if( !useProvidedKeypoints )
  32. {
  33. //t = (double)getTickCount();
  34. findScaleSpaceExtrema(gpyr, dogpyr, keypoints);
  35. //除去重复特征点
  36. KeyPointsFilter::removeDuplicated( keypoints );
  37. // mask标记检测区域(可选)
  38. if( !mask.empty() )
  39. KeyPointsFilter::runByPixelsMask( keypoints, mask );
  40. // retainBest:根据相应保留指定数目的特征点(features2d.hpp)
  41. if( nfeatures > 0 )
  42. KeyPointsFilter::retainBest(keypoints, nfeatures);
  43. //t = (double)getTickCount() - t;
  44. //printf("keypoint detection time: %g\n", t*1000./tf);
  45. }
  46. else
  47. {
  48. // filter keypoints by mask
  49. // KeyPointsFilter::runByPixelsMask( keypoints, mask );
  50. }
  51. // 特征点输出数组
  52. if( _descriptors.needed() )
  53. {
  54. //t = (double)getTickCount();
  55. int dsize = descriptorSize();
  56. _descriptors.create((int)keypoints.size(), dsize, CV_32F);
  57. Mat descriptors = _descriptors.getMat();
  58. calcDescriptors(gpyr, keypoints, descriptors, nOctaveLayers);
  59. //t = (double)getTickCount() - t;
  60. //printf("descriptor extraction time: %g\n", t*1000./tf);
  61. }
  62. }

函数中用到的构造金字塔: buildGaussianPyramid(base, gpyr, nOctaves);等步骤请参见文章后续系列。

OpenCV SIFT原理与源码分析的更多相关文章

  1. 【OpenCV】SIFT原理与源码分析:DoG尺度空间构造

    原文地址:http://blog.csdn.net/xiaowei_cqu/article/details/8067881 尺度空间理论   自然界中的物体随着观测尺度不同有不同的表现形态.例如我们形 ...

  2. 【OpenCV】SIFT原理与源码分析:关键点描述

    <SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 由前一篇<方向赋值>,为找到的关键点即SI ...

  3. 【OpenCV】SIFT原理与源码分析:方向赋值

    <SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 由前一篇<关键点搜索与定位>,我们已经找到 ...

  4. 【OpenCV】SIFT原理与源码分析:关键点搜索与定位

    <SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 由前一步<DoG尺度空间构造>,我们得到了 ...

  5. 【OpenCV】SIFT原理与源码分析

    SIFT简介 Scale Invariant Feature Transform,尺度不变特征变换匹配算法,是由David G. Lowe在1999年(<Object Recognition f ...

  6. OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波

    http://blog.csdn.net/chenyusiyuan/article/details/8710462 OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波 201 ...

  7. ConcurrentHashMap实现原理及源码分析

    ConcurrentHashMap实现原理 ConcurrentHashMap源码分析 总结 ConcurrentHashMap是Java并发包中提供的一个线程安全且高效的HashMap实现(若对Ha ...

  8. HashMap和ConcurrentHashMap实现原理及源码分析

    HashMap实现原理及源码分析 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表, ...

  9. (转)ReentrantLock实现原理及源码分析

    背景:ReetrantLock底层是基于AQS实现的(CAS+CHL),有公平和非公平两种区别. 这种底层机制,很有必要通过跟踪源码来进行分析. 参考 ReentrantLock实现原理及源码分析 源 ...

随机推荐

  1. [python] virtualenv下解决matplotlib中文乱码

    1. 安装中文字体 一般系统自带wqy-microhei,其ttc文件位于/usr/share/fonts/truetype/wqy/wqy-microhei.ttc 2. 将ttc文件复制到pyth ...

  2. JavaScript经典面试题系列

    1.javascript的typeof返回哪些数据类型 Object number function boolean underfind 2.例举3种强制类型转换和2种隐式类型转换? 强制(parse ...

  3. which命令

    which指令会在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果. 格式 which 可执行文件名称 参数 -V  显示版本信息 实例 用 which 去找出 which w ...

  4. php 遍历一个文件夹下的所有文件和子文件夹

    <?php function my_scandir($dir) { $files=array(); if(is_dir($dir)) { if($handle=opendir($dir)) { ...

  5. C语言2048

    这段时间google上有个小游戏挺火的,我也很喜欢,业余时间做个C语言版的. 老规矩先上干货: http://files.cnblogs.com/GhostZCH/2048.rar (.c & ...

  6. asp.net mvc4 Controller与Action执行过程的研究(学习笔记)

    当IIS收到一个http请求,把请求信息发给对应的HttpModel(实际是实现类UrlRoutingModule),在HttpModel中会注册HttpApplication 类中的PostReso ...

  7. Android WebView缓存分析

    http://blog.csdn.net/a345017062/article/details/8703221   WebView的缓存可以分为页面缓存和数据缓存. 页面缓存是指加载一个网页时的htm ...

  8. XE5 开发android平台搭建

    转载自:http://www.cnblogs.com/hezihang/p/3319980.html Delphi XE5的Android开发平台搭建   Delphi XE5支持Android AR ...

  9. 根据WSDL生成代理类方式

    方式一: 1.使用VS2010提供的工具wsdl.exe由WSDL文件生成cs文件 使用wsdl.exe的/serverInterface选项(或缩写的 /si)指定输入的wsdl文件(注意,如果要转 ...

  10. [BZOJ 3531] [Sdoi2014] 旅行 【离线+LCT】

    题目链接:BZOJ - 3531 题目分析 题目询问一条路径上的信息时,每次询问有某种特定的文化的点. 每个点的文化就相当于一种颜色,每次询问一条路径上某种颜色的点的信息. 可以使用离线算法, 类似于 ...