# SFIT特征（四）

414人阅读 评论(0)

6.1采样区域

6.2采样点子区域标号计算

(x,y)T=(cosθsinθsinθcosθ)(x,y)T

(x′′,y′′)T=13σoct(x,y)T+d/2

6.3采样点插值生成种子点向量
Lowe建议，每个采样点梯度采用大小为σ=0.5d$\sigma=0.5d$ 的高斯函数进行处理，即，

mag(a+x,b+y)e(x)2+(y)22(0.5d)2

6.4归一化、设置阈值

6.5C++源代码

/*
* 生成关键点描述符。
* img:高斯图像（按照keypoint的scale高斯滤波的图像）
* ptf:keypoints坐标
* ori：keypoint的主方向
* d/n:keypoints邻域子区域边长为4，每个区域内取8个直方图bins
*/
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));
//转换为弧度值
float cos_t = cosf(ori*(float)(CV_PI/180));
float sin_t = sinf(ori*(float)(CV_PI/180));
float bins_per_rad = n / 360.f;
float exp_scale = -1.f/(d * d * 0.5f);
float hist_width = SIFT_DESCR_SCL_FCTR * scl;//3*\sgima_oct
//采样半径为:3*\sigma_oct*\sqrt(2)*(d+1)
int radius = cvRound(hist_width * 1.4142135623730951f * (d + 1) * 0.5f);
cos_t /= hist_width;
sin_t /= hist_width;

int rows = img.rows, cols = img.cols;

AutoBuffer<float> buf(len*6 + 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 = 0; i < d+2; i++ )
{
for( j = 0; j < d+2; j++ )
for( k = 0; k < n+2; k++ )
hist[(i*(d+2) + j)*(n+2) + k] = 0.;
}

//依次遍历每个采样点
for( i = -radius, k = 0; i <= radius; i++ )
{
// 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.

// 旋转采样点与keypoint的主方向对齐后的位置
float c_rot = j * cos_t - i * sin_t;
float r_rot = j * sin_t + i * cos_t;
float rbin = r_rot + d/2 - 0.5f;
float cbin = c_rot + d/2 - 0.5f;

//采样点在高斯图像中的坐标
int r = pt.y + i, c = pt.x + j;

if( rbin > -1 && rbin < d && cbin > -1 && cbin < d &&
r > 0 && r < rows - 1 && c > 0 && c < cols - 1 )
{
//计算采样点梯度
float dx = (float)(img.at<sift_wt>(r, c+1) - img.at<sift_wt>(r, c-1));
float dy = (float)(img.at<sift_wt>(r-1, c) - img.at<sift_wt>(r+1, 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 = 0; k < len; k++ )
{
float rbin = RBin[k], cbin = CBin[k];
float obin = (Ori[k] - ori)*bins_per_rad;//梯度方向为像素点的方向-关键点主方向（保持旋转不变性）
float mag = Mag[k]*W[k];

//计算采样点距离周围子区域及相邻bin的距离
int r0 = cvFloor( rbin );
int c0 = cvFloor( cbin );
int o0 = cvFloor( obin );
rbin -= r0;
cbin -= c0;
obin -= o0;

if( o0 < 0 )
o0 += n;
if( o0 >= n )
o0 -= n;

// histogram update using tri-linear interpolation(x/y/orientation)
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+1)*(d+2) + c0+1)*(n+2) + o0;
hist[idx] += v_rco000;
hist[idx+1] += v_rco001;
hist[idx+(n+2)] += v_rco010;
hist[idx+(n+3)] += v_rco011;
hist[idx+(d+2)*(n+2)] += v_rco100;
hist[idx+(d+2)*(n+2)+1] += v_rco101;
hist[idx+(d+3)*(n+2)] += v_rco110;
hist[idx+(d+3)*(n+2)+1] += v_rco111;
}
......

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：24700次
• 积分：584
• 等级：
• 排名：千里之外
• 原创：33篇
• 转载：1篇
• 译文：0篇
• 评论：6条
评论排行
最新评论