【OpenCV】SIFT原理与源码分析:关键点描述
《SIFT原理与源码分析》系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html
SIFT描述子h(x,y,θ)是对关键点附近邻域内高斯图像梯度统计的结果,是一个三维矩阵,但通常用一个矢量来表示。矢量通过对三维矩阵按一定规律排列得到。
描述子采样区域


源码
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++;
}
}
计算采样区域梯度直方图

源码
//计算梯度直方图
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原理与源码分析:关键点描述的更多相关文章
- 【OpenCV】SIFT原理与源码分析:关键点搜索与定位
<SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 由前一步<DoG尺度空间构造>,我们得到了 ...
- OpenCV SIFT原理与源码分析
http://blog.csdn.net/xiaowei_cqu/article/details/8069548 SIFT简介 Scale Invariant Feature Transform,尺度 ...
- 【OpenCV】SIFT原理与源码分析:DoG尺度空间构造
原文地址:http://blog.csdn.net/xiaowei_cqu/article/details/8067881 尺度空间理论 自然界中的物体随着观测尺度不同有不同的表现形态.例如我们形 ...
- 【OpenCV】SIFT原理与源码分析:方向赋值
<SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 由前一篇<关键点搜索与定位>,我们已经找到 ...
- 【OpenCV】SIFT原理与源码分析
SIFT简介 Scale Invariant Feature Transform,尺度不变特征变换匹配算法,是由David G. Lowe在1999年(<Object Recognition f ...
- OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波
http://blog.csdn.net/chenyusiyuan/article/details/8710462 OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波 201 ...
- ConcurrentHashMap实现原理及源码分析
ConcurrentHashMap实现原理 ConcurrentHashMap源码分析 总结 ConcurrentHashMap是Java并发包中提供的一个线程安全且高效的HashMap实现(若对Ha ...
- HashMap和ConcurrentHashMap实现原理及源码分析
HashMap实现原理及源码分析 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表, ...
- (转)ReentrantLock实现原理及源码分析
背景:ReetrantLock底层是基于AQS实现的(CAS+CHL),有公平和非公平两种区别. 这种底层机制,很有必要通过跟踪源码来进行分析. 参考 ReentrantLock实现原理及源码分析 源 ...
随机推荐
- 技本功丨请带上纸笔刷着看:解读MySQL执行计划的type列和extra列
本萌最近被一则新闻深受鼓舞,西工大硬核“女学神”白雨桐,获6所世界顶级大学博士录取 货真价值的才貌双全,别人家的孩子 高考失利与心仪的专业失之交臂,选择了软件工程这门自己完全不懂的专业.即便全部归零, ...
- 在Office 365 的如何给管理员赋予查看所有人邮箱的权限的Powershell
连接至Office365 的Powershell Get-MsolUser -UserPrincipalName admin@***.partner.onmschina.cn //Get-MsolUs ...
- 洛谷【P1052】过河
https://www.luogu.org/problemnew/show/P1052 题目描述 在河上有一座长度为 L 的独木桥, 一只青蛙想沿着独木桥从河的一侧跳到另一侧. 在桥上有一些石子, 青 ...
- Amazon.com Seller Distributed Inventory Placement Inventory Placement Service
Greetings, Thank you for writing to us. I understand that you would like to send inventory to our wa ...
- ES6的新特性(4)——字符串的扩展
字符串的扩展 ES6 加强了对 Unicode 的支持,并且扩展了字符串对象. 字符的 Unicode 表示法 JavaScript 允许采用\uxxxx形式表示一个字在\u0000~\uFFFF之间 ...
- 手机上Uncaught ReferenceError: __WEBPACK_AMD_DEFINE_ARRAY__ is not defined的错误
问题: vue2.0和webpack2.X的写的项目在电脑的浏览器上打开正常,在手机浏览器中访问页面的时候不能正常显示,空白. 通过chrome的真机调试chrome://inspect/#devic ...
- PHPDoc 学习记录
https://zh.wikipedia.org/wiki/PHPDoc PHPDoc 是一个 PHP 版的 Javadoc.它是一种注释 PHP 代码的正式标准.它支持通过类似 phpDocumen ...
- 互评Alpha版本——可以低头,但没必要——取件帮
基于NABCD评论作品,及改进建议: 1.根据(不限于)NABCD评论作品的选题 (1)N(Need,需求) 随着电商平台的发展,越来越多的人选择网购,但是东师的一部分快递网点不在校内,需要走很长的一 ...
- Win10系统自带输入法的人机交互设计
过了寒假回校以后,我的电脑重装了系统,为了提升系统运行的速度,自己装了一个内存条同时对硬盘进行了重新的分区,对电脑内的文件也进行了重新的整理,电脑的运行速度提高了很多.老多同学都说win10系统好用, ...
- Croc Champ 2013 - Round 1 E. Copying Data 线段树
题目链接: http://codeforces.com/problemset/problem/292/E E. Copying Data time limit per test2 secondsmem ...