SURF特征描述子对经典SIFT特征进行了改进,SIFT算法的最大缺点是如果不借助硬件或专门的图像处理器很难达到实时。SURF算法的实现原理借鉴了SIFT中的简化近似斯像,把DOH中高斯二阶微分的目标进行简化,使得卷积平滑操作仅需要转换成加减运算,SURF算法的鲁棒性好且时间复杂度低。SURF特征不仅保持SIFT的尺度不变性与选择不变性,而且对光照变化和仿射变化同样具有很强的鲁棒性。
SURF特征通过计算相关Hessian矩阵并找到尺度空间的极值点来确定:
SURF特征向量包含以下三个步骤:(1)构建特征点邻域的正方形区域;(2)将上一步骤建立的正方形区域划分为4 x 4个子区域,在每个子区域里,计算 5 x 5规则网络空间里的4维特征向量,该特征包含Harr小波对水平与垂直方向的响应及响应及响应和的绝对值(3)计算每个子区域的4维并进行累加,将分成的16个子区域进行累计得到最终的64维特征向量描述子。
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\xfeatures2d\nonfree.hpp>
#include <opencv2\imgproc\imgproc.hpp>
cv::Mat SURFFeatureAndCompare(cv::Mat InputImage1, cv::Mat InputImage2)
{
cv::Mat ans;
cv::Ptr<cv::Feature2D> f2d = cv::xfeatures2d::SURF::create();
std::vector<cv::KeyPoint> keypoint1, keypoint2;
cv::Mat descriptors1, descriptors2;
f2d->detect(InputImage1, keypoint1);
f2d->detect(InputImage2, keypoint2);
f2d->compute(InputImage1, keypoint1, descriptors1);
f2d->compute(InputImage2, keypoint2, descriptors2);
cv::BFMatcher matcher;
std::vector<cv::DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
cv::drawMatches(InputImage1, keypoint1, InputImage2, keypoint2, matches, ans);
return ans;
}