【OpenCV】SIFT原理与源码分析

【OpenCV】SIFT原理与源码分析SIFT简介ScaleInvariantFeatureTransform,尺度不变特征变换匹配算法,是由DavidG.Lowe在1999年(《ObjectRecognitionfromLocalScale-InvariantFeatures》)提出的高效区域检测算法,在2004年(《DistinctiveImageFeaturesfromScale-Inva

大家好,又见面了,我是你们的朋友全栈君。

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函数主要有两个接口。

构造函数:

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

nfeatures:特征点数目(算法对检测出的特征点排名,返回最好的nfeatures个特征点)。


nOctaveLayers:金字塔中每组的层数(算法中会自己计算这个值,后面会介绍)。


contrastThreshold:过滤掉较差的特征点的对阈值。contrastThreshold越大,返回的特征点越少。


edgeThreshold:过滤掉边缘效应的阈值。edgeThreshold越大,特征点越多(被多滤掉的越少)。


sigma:金字塔第0层图像高斯滤波系数,也就是σ。


重载操作符:

void SIFT::operator()(InputArray img, InputArray mask, vector<KeyPoint>& keypoints, OutputArraydescriptors, bool useProvidedKeypoints=false)


img:8bit灰度图像


mask:图像检测区域(可选)


keypoints:特征向量矩阵


descipotors:特征点描述的输出向量(如果不需要输出,需要传cv::noArray())。


useProvidedKeypoints:是否进行特征点检测。ture,则检测特征点;false,只计算图像特征描述。


函数源码

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

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

void SIFT::operator()(InputArray _image, InputArray _mask,                      vector<KeyPoint>& keypoints,                      OutputArray _descriptors,                      bool useProvidedKeypoints) const// mask :Optional input mask that marks the regions where we should detect features.// Boolean flag. If it is true, the keypoint detector is not run. Instead,// the provided vector of keypoints is used and the algorithm just computes their descriptors.// descriptors – The output matrix of descriptors.// Pass cv::noArray() if you do not need them.			  {    Mat image = _image.getMat(), mask = _mask.getMat();    if( image.empty() || image.depth() != CV_8U )        CV_Error( CV_StsBadArg, "image is empty or has incorrect depth (!=CV_8U)" );    if( !mask.empty() && mask.type() != CV_8UC1 )        CV_Error( CV_StsBadArg, "mask has incorrect type (!=CV_8UC1)" );			// 得到第1组(Octave)图像    Mat base = createInitialImage(image, false, (float)sigma);    vector<Mat> gpyr, dogpyr;	// 每层金字塔图像的组数(Octave)    int nOctaves = cvRound(log( (double)std::min( base.cols, base.rows ) ) / log(2.) - 2);    // double t, tf = getTickFrequency();    // t = (double)getTickCount();		// 构建金字塔(金字塔层数和组数相等)    buildGaussianPyramid(base, gpyr, nOctaves);	// 构建高斯差分金字塔    buildDoGPyramid(gpyr, dogpyr);    //t = (double)getTickCount() - t;    //printf("pyramid construction time: %g\n", t*1000./tf);		// useProvidedKeypoints默认为false	// 使用keypoints并计算特征点的描述符    if( !useProvidedKeypoints )    {        //t = (double)getTickCount();        findScaleSpaceExtrema(gpyr, dogpyr, keypoints);		//除去重复特征点        KeyPointsFilter::removeDuplicated( keypoints ); 		// mask标记检测区域(可选)        if( !mask.empty() )            KeyPointsFilter::runByPixelsMask( keypoints, mask );		// retainBest:根据相应保留指定数目的特征点(features2d.hpp)        if( nfeatures > 0 )            KeyPointsFilter::retainBest(keypoints, nfeatures);        //t = (double)getTickCount() - t;        //printf("keypoint detection time: %g\n", t*1000./tf);    }    else    {        // filter keypoints by mask        // KeyPointsFilter::runByPixelsMask( keypoints, mask );    }	// 特征点输出数组    if( _descriptors.needed() )    {        //t = (double)getTickCount();        int dsize = descriptorSize();        _descriptors.create((int)keypoints.size(), dsize, CV_32F);        Mat descriptors = _descriptors.getMat();        calcDescriptors(gpyr, keypoints, descriptors, nOctaveLayers);        //t = (double)getTickCount() - t;        //printf("descriptor extraction time: %g\n", t*1000./tf);    }}

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



(转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/151699.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • .NET中pdb文件的作用是什么「建议收藏」

    .NET中pdb文件的作用是什么「建议收藏」.PDB是ProgramDatabase的缩写,全称为“程序数据库”文件。我们使用它(更确切的说是看到它被应用)大多数场景是调试应用程序。目前我们对.PDB文件的普遍认知是它存储了被编译文件的调试信息,作为符号文件存在。 PDB文件寻路 如果我们观察VS启动调试加载模块和符号文件的过程,会发现它通常会从可执行文件或者DLL文件的相同目录中加载符号文件。这正是调试器寻找PDB文件的

  • Sicily 1700. Ping

    Sicily 1700. Ping

  • Extjs弹窗控件——Ext.MessageBox

    Extjs弹窗控件——Ext.MessageBox首先,浏览器自带的弹窗有alert、confirm、prompt等。js弹窗的3种方式:alert、confirm、prompt鉴于其外观丑陋以及配置不方便,我们常采用Extjs自带的弹窗控件。//

  • 在C#中ParameterizedThreadStart和ThreadStart区别

    在C#中ParameterizedThreadStart和ThreadStart区别
    不需要传递参数,也不需要返回参数
      我们知道启动一个线程最直观的办法是使用Thread类,具体步骤如下:
    ThreadStartthreadStart=newThreadStart(Calculate);Threadthread=newThread(threadStart);thread.Start();publicvoidCalculate(){ doubleDiameter=0.5; Console.Write(“T

  • MyBatis——动态SQL总结

    MyBatis——动态SQL总结MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。 MyBatis中用于实现动态SQL的元素主要有:ifwheresetchoose(when,otherwise)trimforeach   (1)if标签此时如果CNAINDCLABASINFID为null,此语句很可能报错或查询结果为空。此时我们使用if动态sql语句先进行判断,如果值为null…

  • 腾讯面试题目汇总[通俗易懂]

    腾讯面试题目汇总面试官提问1:自我介绍及项目经历关于这道题,每个人的项目经历都不太一样,所以各位朋友根据自己的实际情况来介绍吧,在这里就不多介绍了。面试官提问2:看你项目介绍中大量使用了Redis,那能不能介绍下Redis的主从同步机制呢?关于这道题,因为我在之前的文章也分析过Redis主从同步的机制,所以我从完整重同步和部分重同步两个阶段去分析的,结果也得到了面试官的认可。详细的完整重同步和部分重同步机制原理是什么样的,在这里就不展开介绍了,附上链接朋友们自行查…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号