我的第一个的
我现在在做行人检测的,在此下载了一些行人检测的代码的,基于matlab版本的,但是不是很理解的,希望请教大家的:
这个三个代码怎么的运行的???
第一个代码:用来计算binhogfeature
function binfeat = BinHOGFeature(blockGr, blockInd, CellSize, BinNum)
% devide the block
block_ori1=blockGr(1:CellSize,1:CellSize);
block_ori2=blockGr(1:CellSize,(1+CellSize):2*CellSize);
block_ori3=blockGr((1+CellSize):2*CellSize,1:CellSize);
block_ori4=blockGr((1+CellSize):2*CellSize,(1+CellSize):2*CellSize);
block_grad1=blockInd(1:CellSize,1:CellSize);
block_grad2=blockInd(1:CellSize,(1+CellSize):2*CellSize);
block_grad3=blockInd((1+CellSize):2*CellSize,1:CellSize);
block_grad4=blockInd((1+CellSize):2*CellSize,(1+CellSize):2*CellSize);
% here we calculate 4 cells
binfeat = zeros(BinNum*4, 1);
feat1 = zeros(BinNum, 1);
feat2 = zeros(BinNum, 1);
feat3 = zeros(BinNum, 1);
feat4 = zeros(BinNum, 1);
for i=1:BinNum
feat1(i) = sum(block_ori1(find(block_grad1==i)));
end
for i=1:BinNum
feat2(i) = sum(block_ori2(find(block_grad2==i)));
end
for i=1:BinNum
feat3(i) = sum(block_ori3(find(block_grad3==i)));
end
for i=1:BinNum
feat4(i) = sum(block_ori4(find(block_grad4==i)));
end
binfeat = [feat1;feat2;feat3;feat4];
% binfeat = binfeat./sum(binfeat); % here we normallize the feature
sump=sqrt(sum(binfeat.^2));
binfeat = binfeat./(sump+eps);
第二个代码:HOGGradient
function [gradx, grady] = HOGGradient(G)
% here we defing the mask as [-1, 0, 1], the edge of the image use [-1, 1]
[height, width] = size(G);
gradx = zeros(height, width);
grady = zeros(height, width);
% Process the Image edge
gradx(:,1) = sum(G(:,1:2).*repmat([-1, 1], height, 1), 2);
gradx(:,width) = sum(G(:, width-1:width).*repmat([-1, 1], height, 1), 2);
grady(1,:) = sum(G(1:2, :).*repmat([-1; 1], 1, width), 1);
grady(height,:) = sum(G(height-1:height, :).*repmat([-1; 1], 1, width), 1);
for j=2:height-1
for i=2:width-1
tmpx = G(j, i-1:i+1);
tmpy = G(j-1:j+1, i);
gradx(j,i) = sum(tmpx.*[-1, 0, 1], 2);
grady(j,i) = sum(tmpy.*[-1; 0; 1], 1);
end
end
第三个代码: ImgHOGFeature
function [imgHOGFeature] = ImgHOGFeature(ImgPathName, SkipStep, BinNum, Angle, CellSize, filter_para)
% compare with ImgHOGFeature2, this function use image name instead of Image data
if nargin<5
disp('Not enough parameters!');
return;
end
if nargin==6
filter_hsize=filter_para(1);
filter_delta=filter_para(2);
end
%% Gamma/Colour Normalization
Img = imread(ImgPathName);
if size(Img,3) == 3
G = rgb2gray(Img);
else
G = Img;
end
[height, width] = size(G);
%% Gradient and Gradient angle Computation
if filter_hsize == 0
[GradientX,GradientY] = gradient(double(G));
else
h=fspecial('gaussian', filter_hsize, filter_delta);
[GradientX, GradientY] = HOGGradient(double(G));
end
% calculate the norm of gradient
Gr = sqrt(GradientX.^2+GradientY.^2);
% Calculate the angle
index = find(GradientX == 0);
GradientX(index) = 1e-5;
YX = GradientY./GradientX;
if Angle == 180, A = ((atan(YX)+(pi/2))*180)./pi; end
if Angle == 360, A = ((atan2(GradientY,GradientX)+pi).*180)./pi; end
%% Spatial / Orientation Binning
nAngle = Angle/BinNum;
IndTag = ceil(A./nAngle);
xStepNum = floor((width-2*CellSize)/SkipStep+1);
yStepNum = floor((height-2*CellSize)/SkipStep+1);
overL = SkipStep;
FeatDim = BinNum*4;
imgHOGFeature = zeros(FeatDim, xStepNum*yStepNum);
currFeat = zeros(FeatDim,1);
% BinHOG = inline('BinHOGFeature(blockGr, blockInd, CellSize, BinNum)', 'blockGr', 'blockInd', 'CellSize', 'BinNum');
for j=1:xStepNum
for k=1:yStepNum
x_Off = (j-1)*overL+1;
y_Off = (k-1)*overL+1;
% here we define each block have 4 cells
blockGr = Gr(y_Off:y_Off+2*CellSize-1,x_Off:x_Off+2*CellSize-1);
blockInd = IndTag(y_Off:y_Off+2*CellSize-1,x_Off:x_Off+2*CellSize-1);
% calculate the block tag and Grain of each pixel
currFeat = BinHOGFeature(blockGr, blockInd, CellSize, BinNum);
% calculate the feature of the block
imgHOGFeature(:, (j-1)*yStepNum+k) = currFeat;
end
end