# 几个问题

HOG特征也是人工设计的特征，既然是人工设计，那就一定包含了作者的深厚内力在里面，也是充满了经验主义，更多的是实验论证，疗效是关键，而不是理论上的优雅。关于它为什么就这么好，原作者本人也是做了经验上和工程上的分析。

# 原理框架

## 输入图像

HOG特征与SIFT特征不同的是它检测窗口尺度固定，通过降采样原始图片来获得与窗口尺寸匹配的输入图像，而SIFT把这部分直接做到了检测算子的构造流程中去。论文中选择64*128的窗口也是根据数据集的特点和待检测目标的性质决定的。

LAB 近似无区别

## 计算梯度（良好的梯度尺度）

1-D point derivatives [1, 0, -1] σ=0 baseline
Cubic corrected 1-D [1, -8, 0, 8, -1] σ=0 ↓1%
1-D point derivatives [-1,1] σ=0 ↓1.5%
2×2 diagonal ones σ=0 ↓1.5%
σ=2 ↓9%

# 论文对应代码实现

function [feature] = hog_feature_vector(im)

% The given code finds the HOG feature vector for any given image. HOG
% feature vector/descriptor can then be used for detection of any
% particular object. The Matlab code provides the exact implementation of
% the formation of HOG feature vector as detailed in the paper "Pedestrian
% detection using HOG" by Dalal and Triggs

% INPUT => im (input image)
% OUTPUT => HOG feature vector for that particular image

% Example: Running the code
% >>> hog = hog_feature_vector (im);

% Convert RGB iamge to grayscale
if size(im,3)==3
im=rgb2gray(im);
end
im=double(im);

rows=size(im,1);
cols=size(im,2);
Ix=im; %Basic Matrix assignment
Iy=im; %Basic Matrix assignment

% Gradients in X and Y direction. Iy is the gradient in X direction and Iy
% is the gradient in Y direction
for i=1:rows-2
Iy(i,:)=(im(i,:)-im(i+2,:));
end
for i=1:cols-2
Ix(:,i)=(im(:,i)-im(:,i+2));
end

gauss=fspecial('gaussian',8); %% Initialized a gaussian filter with sigma=0.5 * block width.

angle=atand(Ix./Iy); % Matrix containing the angles of each edge gradient
magnitude=sqrt(Ix.^2 + Iy.^2);

% figure,imshow(uint8(angle));
% figure,imshow(uint8(magnitude));

% Remove redundant pixels in an image.
angle(isnan(angle))=0;
magnitude(isnan(magnitude))=0;

feature=[]; %initialized the feature vector

% Iterations for Blocks
for i = 0: rows/8 - 2
for j= 0: cols/8 -2
%disp([i,j])

mag_patch = magnitude(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
%mag_patch = imfilter(mag_patch,gauss);
ang_patch = angle(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);

block_feature=[];

%Iterations for cells in a block
for x= 0:1
for y= 0:1
angleA =ang_patch(8*x+1:8*x+8, 8*y+1:8*y+8);
magA   =mag_patch(8*x+1:8*x+8, 8*y+1:8*y+8);
histr  =zeros(1,9);

%Iterations for pixels in one cell
for p=1:8
for q=1:8
%
alpha= angleA(p,q);

% Binning Process (Bi-Linear Interpolation)
if alpha>10 && alpha<=30
histr(1)=histr(1)+ magA(p,q)*(30-alpha)/20;
histr(2)=histr(2)+ magA(p,q)*(alpha-10)/20;
elseif alpha>30 && alpha<=50
histr(2)=histr(2)+ magA(p,q)*(50-alpha)/20;
histr(3)=histr(3)+ magA(p,q)*(alpha-30)/20;
elseif alpha>50 && alpha<=70
histr(3)=histr(3)+ magA(p,q)*(70-alpha)/20;
histr(4)=histr(4)+ magA(p,q)*(alpha-50)/20;
elseif alpha>70 && alpha<=90
histr(4)=histr(4)+ magA(p,q)*(90-alpha)/20;
histr(5)=histr(5)+ magA(p,q)*(alpha-70)/20;
elseif alpha>90 && alpha<=110
histr(5)=histr(5)+ magA(p,q)*(110-alpha)/20;
histr(6)=histr(6)+ magA(p,q)*(alpha-90)/20;
elseif alpha>110 && alpha<=130
histr(6)=histr(6)+ magA(p,q)*(130-alpha)/20;
histr(7)=histr(7)+ magA(p,q)*(alpha-110)/20;
elseif alpha>130 && alpha<=150
histr(7)=histr(7)+ magA(p,q)*(150-alpha)/20;
histr(8)=histr(8)+ magA(p,q)*(alpha-130)/20;
elseif alpha>150 && alpha<=170
histr(8)=histr(8)+ magA(p,q)*(170-alpha)/20;
histr(9)=histr(9)+ magA(p,q)*(alpha-150)/20;
elseif alpha>=0 && alpha<=10
histr(1)=histr(1)+ magA(p,q)*(alpha+10)/20;
histr(9)=histr(9)+ magA(p,q)*(10-alpha)/20;
elseif alpha>170 && alpha<=180
histr(9)=histr(9)+ magA(p,q)*(190-alpha)/20;
histr(1)=histr(1)+ magA(p,q)*(alpha-170)/20;
end

end
end
block_feature=[block_feature histr]; % Concatenation of Four histograms to form one block feature

end
end
% Normalize the values in the block using L2-Norm
block_feature=block_feature/sqrt(norm(block_feature)^2+.01);

feature=[feature block_feature]; %Features concatenation
end
end

feature(isnan(feature))=0; %Removing Infinitiy values

% Normalization of the feature vector using L2-Norm
feature=feature/sqrt(norm(feature)^2+.001);
for z=1:length(feature)
if feature(z)>0.2
feature(z)=0.2;
end
end

%renormalization
feature=feature/sqrt(norm(feature)^2+.001);

% toc;       

# 延伸

DPM算法
OpenCV人脸识别中HOG特征的实现

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120