《SIFT原理与源码分析》系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html

由前一篇《方向赋值》,为找到的关键点即SIFT特征点赋了值,包含位置、尺度和方向的信息。接下来的步骤是关键点描述,即用用一组向量将这个关键点描述出来,这个描述子不但包括关键点,也包括关键点周围对其有贡献的像素点。用来作为目标匹配的依据(所以描述子应该有较高的独特性,以保证匹配率),也可使关键点具有更多的不变特性,如光照变化、3D视点变化等。

SIFT描述子h(x,y,θ)是对关键点附近邻域内高斯图像梯度统计的结果,是一个三维矩阵,但通常用一个矢量来表示。矢量通过对三维矩阵按一定规律排列得到。

描述子采样区域

特征描述子与关键点所在尺度有关,因此对梯度的求取应在特征点对应的高斯图像上进行。将关键点附近划分成d×d个子区域,每个子区域尺寸为mσ个像元(d=4,m=3,σ为尺特征点的尺度值)。考虑到实际计算时需要双线性插值,故计算的图像区域为mσ(d+1),再考虑旋转,则实际计算的图像区域为,如下图所示:

源码

    Point pt(cvRound(ptf.x), cvRound(ptf.y));
//计算余弦,正弦,CV_PI/180:将角度值转化为幅度值
float cos_t = cosf(ori*(float)(CV_PI/));
float sin_t = sinf(ori*(float)(CV_PI/));
float bins_per_rad = n / .f;
float exp_scale = -.f/(d * d * 0.5f); //d:SIFT_DESCR_WIDTH 4
float hist_width = SIFT_DESCR_SCL_FCTR * scl; // SIFT_DESCR_SCL_FCTR: 3
// scl: size*0.5f
// 计算图像区域半径mσ(d+1)/2*sqrt(2)
// 1.4142135623730951f 为根号2
int radius = cvRound(hist_width * 1.4142135623730951f * (d + ) * 0.5f);
cos_t /= hist_width;
sin_t /= hist_width;

区域坐标轴旋转

为了保证特征矢量具有旋转不变性,要以特征点为中心,在附近邻域内旋转θ角,即旋转为特征点的方向。
旋转后区域内采样点新的坐标为:

源码

//计算采样区域点坐标旋转
for( i = -radius, k = ; i <= radius; i++ )
for( j = -radius; j <= radius; j++ )
{
/*
Calculate sample's histogram array coords rotated relative to ori.
Subtract 0.5 so samples that fall e.g. in the center of row 1 (i.e.
r_rot = 1.5) have full weight placed in row 1 after interpolation.
*/
float c_rot = j * cos_t - i * sin_t;
float r_rot = j * sin_t + i * cos_t;
float rbin = r_rot + d/ - 0.5f;
float cbin = c_rot + d/ - 0.5f;
int r = pt.y + i, c = pt.x + j; if( rbin > - && rbin < d && cbin > - && cbin < d &&
r > && r < rows - && c > && c < cols - )
{
float dx = (float)(img.at<short>(r, c+) - img.at<short>(r, c-));
float dy = (float)(img.at<short>(r-, c) - img.at<short>(r+, c));
X[k] = dx; Y[k] = dy; RBin[k] = rbin; CBin[k] = cbin;
W[k] = (c_rot * c_rot + r_rot * r_rot)*exp_scale;
k++;
}
}

计算采样区域梯度直方图

将旋转后区域划分为d×d个子区域(每个区域间隔为mσ像元),在子区域内计算8个方向的梯度直方图,绘制每个方向梯度方向的累加值,形成一个种子点。
与求主方向不同的是,此时,每个子区域梯度方向直方图将0°~360°划分为8个方向区间,每个区间为45°。即每个种子点有8个方向区间的梯度强度信息。由于存在d×d,即4×4个子区域,所以最终共有4×4×8=128个数据,形成128维SIFT特征矢量。
对特征矢量需要加权处理,加权采用mσd/2的标准高斯函数。为了除去光照变化影响,还有一步归一化处理。

源码

//计算梯度直方图
for( k = ; k < len; k++ )
{
float rbin = RBin[k], cbin = CBin[k];
float obin = (Ori[k] - ori)*bins_per_rad;
float mag = Mag[k]*W[k]; int r0 = cvFloor( rbin );
int c0 = cvFloor( cbin );
int o0 = cvFloor( obin );
rbin -= r0;
cbin -= c0;
obin -= o0; //n为SIFT_DESCR_HIST_BINS:8,即将360°分为8个区间
if( o0 < )
o0 += n;
if( o0 >= n )
o0 -= n; // histogram update using tri-linear interpolation
// 双线性插值
float v_r1 = mag*rbin, v_r0 = mag - v_r1;
float v_rc11 = v_r1*cbin, v_rc10 = v_r1 - v_rc11;
float v_rc01 = v_r0*cbin, v_rc00 = v_r0 - v_rc01;
float v_rco111 = v_rc11*obin, v_rco110 = v_rc11 - v_rco111;
float v_rco101 = v_rc10*obin, v_rco100 = v_rc10 - v_rco101;
float v_rco011 = v_rc01*obin, v_rco010 = v_rc01 - v_rco011;
float v_rco001 = v_rc00*obin, v_rco000 = v_rc00 - v_rco001; int idx = ((r0+)*(d+) + c0+)*(n+) + o0;
hist[idx] += v_rco000;
hist[idx+] += v_rco001;
hist[idx+(n+)] += v_rco010;
hist[idx+(n+)] += v_rco011;
hist[idx+(d+)*(n+)] += v_rco100;
hist[idx+(d+)*(n+)+] += v_rco101;
hist[idx+(d+)*(n+)] += v_rco110;
hist[idx+(d+)*(n+)+] += v_rco111;
}

关键点描述源码

// SIFT关键点特征描述
// SIFT描述子是关键点领域高斯图像提取统计结果的一种表示
static void calcSIFTDescriptor( const Mat& img, Point2f ptf, float ori, float scl,
int d, int n, float* dst ) {
Point pt(cvRound(ptf.x), cvRound(ptf.y));
//计算余弦,正弦,CV_PI/180:将角度值转化为幅度值
float cos_t = cosf(ori*(float)(CV_PI/));
float sin_t = sinf(ori*(float)(CV_PI/));
float bins_per_rad = n / .f;
float exp_scale = -.f/(d * d * 0.5f); //d:SIFT_DESCR_WIDTH 4
float hist_width = SIFT_DESCR_SCL_FCTR * scl; // SIFT_DESCR_SCL_FCTR: 3
// scl: size*0.5f
// 计算图像区域半径mσ(d+1)/2*sqrt(2)
// 1.4142135623730951f 为根号2
int radius = cvRound(hist_width * 1.4142135623730951f * (d + ) * 0.5f);
cos_t /= hist_width;
sin_t /= hist_width; int i, j, k, len = (radius*+)*(radius*+), histlen = (d+)*(d+)*(n+);
int rows = img.rows, cols = img.cols; AutoBuffer<float> buf(len* + histlen);
float *X = buf, *Y = X + len, *Mag = Y, *Ori = Mag + len, *W = Ori + len;
float *RBin = W + len, *CBin = RBin + len, *hist = CBin + len; //初始化直方图
for( i = ; i < d+; i++ )
{
for( j = ; j < d+; j++ )
for( k = ; k < n+; k++ )
hist[(i*(d+) + j)*(n+) + k] = .;
} //计算采样区域点坐标旋转
for( i = -radius, k = ; i <= radius; i++ )
for( j = -radius; j <= radius; j++ )
{
/*
Calculate sample's histogram array coords rotated relative to ori.
Subtract 0.5 so samples that fall e.g. in the center of row 1 (i.e.
r_rot = 1.5) have full weight placed in row 1 after interpolation.
*/
float c_rot = j * cos_t - i * sin_t;
float r_rot = j * sin_t + i * cos_t;
float rbin = r_rot + d/ - 0.5f;
float cbin = c_rot + d/ - 0.5f;
int r = pt.y + i, c = pt.x + j; if( rbin > - && rbin < d && cbin > - && cbin < d &&
r > && r < rows - && c > && c < cols - )
{
float dx = (float)(img.at<short>(r, c+) - img.at<short>(r, c-));
float dy = (float)(img.at<short>(r-, c) - img.at<short>(r+, c));
X[k] = dx; Y[k] = dy; RBin[k] = rbin; CBin[k] = cbin;
W[k] = (c_rot * c_rot + r_rot * r_rot)*exp_scale;
k++;
}
} len = k;
fastAtan2(Y, X, Ori, len, true);
magnitude(X, Y, Mag, len);
exp(W, W, len); //计算梯度直方图
for( k = ; k < len; k++ )
{
float rbin = RBin[k], cbin = CBin[k];
float obin = (Ori[k] - ori)*bins_per_rad;
float mag = Mag[k]*W[k]; int r0 = cvFloor( rbin );
int c0 = cvFloor( cbin );
int o0 = cvFloor( obin );
rbin -= r0;
cbin -= c0;
obin -= o0; //n为SIFT_DESCR_HIST_BINS:8,即将360°分为8个区间
if( o0 < )
o0 += n;
if( o0 >= n )
o0 -= n; // histogram update using tri-linear interpolation
// 双线性插值
float v_r1 = mag*rbin, v_r0 = mag - v_r1;
float v_rc11 = v_r1*cbin, v_rc10 = v_r1 - v_rc11;
float v_rc01 = v_r0*cbin, v_rc00 = v_r0 - v_rc01;
float v_rco111 = v_rc11*obin, v_rco110 = v_rc11 - v_rco111;
float v_rco101 = v_rc10*obin, v_rco100 = v_rc10 - v_rco101;
float v_rco011 = v_rc01*obin, v_rco010 = v_rc01 - v_rco011;
float v_rco001 = v_rc00*obin, v_rco000 = v_rc00 - v_rco001; int idx = ((r0+)*(d+) + c0+)*(n+) + o0;
hist[idx] += v_rco000;
hist[idx+] += v_rco001;
hist[idx+(n+)] += v_rco010;
hist[idx+(n+)] += v_rco011;
hist[idx+(d+)*(n+)] += v_rco100;
hist[idx+(d+)*(n+)+] += v_rco101;
hist[idx+(d+)*(n+)] += v_rco110;
hist[idx+(d+)*(n+)+] += v_rco111;
} // finalize histogram, since the orientation histograms are circular
// 最后确定直方图,目标方向直方图是圆的
for( i = ; i < d; i++ )
for( j = ; j < d; j++ )
{
int idx = ((i+)*(d+) + (j+))*(n+);
hist[idx] += hist[idx+n];
hist[idx+] += hist[idx+n+];
for( k = ; k < n; k++ )
dst[(i*d + j)*n + k] = hist[idx+k];
}
// copy histogram to the descriptor,
// apply hysteresis thresholding
// and scale the result, so that it can be easily converted
// to byte array
float nrm2 = ;
len = d*d*n;
for( k = ; k < len; k++ )
nrm2 += dst[k]*dst[k];
float thr = std::sqrt(nrm2)*SIFT_DESCR_MAG_THR;
for( i = , nrm2 = ; i < k; i++ )
{
float val = std::min(dst[i], thr);
dst[i] = val;
nrm2 += val*val;
}
nrm2 = SIFT_INT_DESCR_FCTR/std::max(std::sqrt(nrm2), FLT_EPSILON);
for( k = ; k < len; k++ )
{
dst[k] = saturate_cast<uchar>(dst[k]*nrm2);
}
}

至此SIFT描述子生成,SIFT算法也基本完成了~参见《SIFT原理与源码分析

【OpenCV】SIFT原理与源码分析:关键点描述的更多相关文章

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

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

  2. OpenCV SIFT原理与源码分析

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

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

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

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

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

  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. java使用jacob将office文档转换为PDF格式

    jacob 包下载地址: http://sourceforge.net/projects/jacob-project/ 下载后,将jacob 与 jacob-1.19-x64.dll放到安装jdk目录 ...

  2. 数据时代的的企业管理 记SAP商业同略会

    [PConline 资讯]在2012 SAP中国商业同略会城市论坛深圳站上,自SAP中国的萧洁云总裁和张志琦先生,对SAP中国的战略.SAP的技术战略,以及SAP对于行业趋势分析与媒体进行了沟通,对数 ...

  3. MySQL case when 使用

    case when 自定义排序时的使用 根据 case when 新的 sort字段排序 case when t2.status = 4 and t2.expire_time>UNIX_TIME ...

  4. 基于spec评论作品 - 探路者 贪吃蛇

    基于spec评论作品,试用(并截图)所有其他小组的Alpha作品,与软件功能说明书对比,评论Alpha作品对软件功能说明书的实现. 首先通过命令行进入到游戏主页面中. 因为软件没有编译为exe程序,所 ...

  5. 01慕课网《进击Node.js基础(一)》Node.js安装,创建例子

    版本:偶数位为稳定版本,基数为非稳定版本 - 0.6.x - 0.7.x    - 0.8.x -0.9.x    -0.10.x  -0.11.x 概念:Node.js采用谷歌浏览器的V8引擎,用C ...

  6. 配置EditPlus编辑器使其成为Python的编辑、执行环境

    1.添加Python群组 运行EditPlus,选择工具→配置用户工具进入参数设置框. 单击添加工具→应用程序.菜单文字输入python,命令为Python的安装路径,参数输入 $(FileName) ...

  7. VUE AXIOS 跨域问题

    背景: 后台跨域使用通配符:context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); ...

  8. C语言的问卷调查

    1.你对自己的未来有什么规划?做了哪些准备? 未来想当一个网络工程师,为了这个目标我正在努力学习网络.网页及相关的知识. 2.你认为什么是学习?学习有什么用?现在学习动力如何?为什么? 学习就是不断尝 ...

  9. 实现Spring管理struts的Action

    struts2和spring的整合,关键点在于struts2中的action要纳入spring容器的管理中成为一个bean.  可以在struts2中配置:  <struts>      ...

  10. 1 RabbitMQ 安装,配置

    1:安装 yum install -y rabbitmq-server   2:主要程序介绍 # 管理插件的程序 /usr/sbin/rabbitmq-plugins # 服务程序 /usr/sbin ...