function [feature] = hog_feature_calc(image)
% function: calculate hog_feature_vector
% 输入:image
% 输出:hog feature vector
% Example: Running the code
% >>> im = imread('001.jpg');
% >>> hog = hog_feature_calc(im);
% 第1步:计算水平,竖直方向, 像素梯度矩阵:Ix、Ty
% 第2步:计算image对应:angle,magnitude矩阵
% 第3步:遍历, 第1层遍历block, 第2层遍历cell, 第3层遍历pixel
% 计算每个pixel直方图,直方图合并
% 第4步:L2-Norm归一化, 0.2截断, 再L2-Norm归一化
% Convert RGB image to grayscale
if size(image,3)==3
im=rgb2gray(image);
end
im=double(im);
rows=size(im,1);cols=size(im,2);
Ix=im; Iy=im;
% Gradients in X and Y direction.
for j=1:cols-2
Ix(:,j)=(im(:,j)-im(:,j+2));
end
for i=1:rows-2
Iy(i,:)=(im(i,:)-im(i+2,:));
end
figure(1);imshow(Ix);
figure(2);imshow(Iy);
angle=atand(Ix./Iy);
angle=imadd(angle,90);
magnitude=sqrt(Ix.^2 + Iy.^2);
angle(isnan(angle))=0;
magnitude(isnan(magnitude))=0;
figure(3);imshow(uint8(angle));
figure(4);imshow(uint8(magnitude));
feature=[]; % initialized the hog_feature vector
% Iterations for Blocks
for i = 0: rows/8 - 2
for j= 0: cols/8 -2
mag_patch = magnitude(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
ang_patch = angle(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
block_feature=[]; % initialized block_feature
%Iterations for cells in a block
for x= 0:1
for y= 0:1
angle_cell =ang_patch(8*x+1:8*x+8, 8*y+1:8*y+8);
mag_cell =mag_patch(8*x+1:8*x+8, 8*y+1:8*y+8);
hist_cell =zeros(1,9);
%Iterations for pixels in one cell
for p=1:8
for q=1:8
%
alpha= angle_cell(p,q);
% Binning Process (Bi-Linear Interpolation)
if alpha>10 && alpha<=30
hist_cell(1)=hist_cell(1)+ mag_cell(p,q)*(30-alpha)/20;
hist_cell(2)=hist_cell(2)+ mag_cell(p,q)*(alpha-10)/20;
elseif alpha>30 && alpha<=50
hist_cell(2)=hist_cell(2)+ mag_cell(p,q)*(50-alpha)/20;
hist_cell(3)=hist_cell(3)+ mag_cell(p,q)*(alpha-30)/20;
elseif alpha>50 && alpha<=70
hist_cell(3)=hist_cell(3)+ mag_cell(p,q)*(70-alpha)/20;
hist_cell(4)=hist_cell(4)+ mag_cell(p,q)*(alpha-50)/20;
elseif alpha>70 && alpha<=90
hist_cell(4)=hist_cell(4)+ mag_cell(p,q)*(90-alpha)/20;
hist_cell(5)=hist_cell(5)+ mag_cell(p,q)*(alpha-70)/20;
elseif alpha>90 && alpha<=110
hist_cell(5)=hist_cell(5)+ mag_cell(p,q)*(110-alpha)/20;
hist_cell(6)=hist_cell(6)+ mag_cell(p,q)*(alpha-90)/20;
elseif alpha>110 && alpha<=130
hist_cell(6)=hist_cell(6)+ mag_cell(p,q)*(130-alpha)/20;
hist_cell(7)=hist_cell(7)+ mag_cell(p,q)*(alpha-110)/20;
elseif alpha>130 && alpha<=150
hist_cell(7)=hist_cell(7)+ mag_cell(p,q)*(150-alpha)/20;
hist_cell(8)=hist_cell(8)+ mag_cell(p,q)*(alpha-130)/20;
elseif alpha>150 && alpha<=170
hist_cell(8)=hist_cell(8)+ mag_cell(p,q)*(170-alpha)/20;
hist_cell(9)=hist_cell(9)+ mag_cell(p,q)*(alpha-150)/20;
elseif alpha>=0 && alpha<=10
hist_cell(1)=hist_cell(1)+ mag_cell(p,q)*(alpha+10)/20;
hist_cell(9)=hist_cell(9)+ mag_cell(p,q)*(10-alpha)/20;
elseif alpha>170 && alpha<=180
hist_cell(9)=hist_cell(9)+ mag_cell(p,q)*(190-alpha)/20;
hist_cell(1)=hist_cell(1)+ mag_cell(p,q)*(alpha-170)/20;
end
end
end
% Concatenation of Four histograms to form one block feature
block_feature=[block_feature hist_cell];
end
end
% Normalize the values in the block using L2-Norm
block_feature=block_feature/sqrt(norm(block_feature)^2+.01);
% Features concatenation
feature=[feature block_feature];
end
end
feature(isnan(feature))=0;
% 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);