SrcImg = imread('lena.jpg');
figure('Name','Hog');
subplot(2,2,1); imshow(SrcImg); title('SrcImg');
[H,W,D] = size(SrcImg);
if(D ~= 1)
GrayImg = rgb2gray(SrcImg);
else
GrayImg = SrcImg;
end
subplot(2,2,2); imshow(GrayImg); title('GrayImg');
GrayImg = double(GrayImg);
Gx = zeros(W,H);
Gy = zeros(W,H);
for i = 2:H - 1
for j = 2:W - 1
Gx(i,j) = GrayImg(i,j + 1) - GrayImg(i,j - 1);
Gy(i,j) = GrayImg(i + 1,j) - GrayImg(i - 1,j);
end
end
Gxy = sqrt(Gx .^ 2 + Gy .^ 2);
subplot(2,2,3); imshow(Gxy); title('Gxy');
Axy = (atan(Gy ./ Gx) / pi) * 180;
Axy(isnan(Axy)) = 0;
Axy(find(Axy < 0)) = Axy(find(Axy < 0)) + 180;
Axy = floor((Axy / 20) + 1);
subplot(2,2,4); imshow(Axy); title('Axy');
BinNum = 9;
CellStep = 4;
Hist = zeros(H / CellStep, W / CellStep, BinNum);
TempAxy = zeros(CellStep,CellStep);
TempGxy = zeros(CellStep,CellStep);
for i = 1:H / CellStep
for j = 1:W / CellStep
TempAxy = Axy((i - 1) * CellStep + 1:i * CellStep,(j - 1) * CellStep + 1:j * CellStep);
TempGxy = Gxy((i - 1) * CellStep + 1:i * CellStep,(j - 1) * CellStep + 1:j * CellStep);
for w = 1:CellStep
for l = 1:CellStep
Hist(i,j,TempAxy(w,l)) = Hist(i,j,TempAxy(w,l)) + TempGxy(w,l);
end
end
end
end
% CellNumH and CellNumW is the number of cell in every row and column.
CellNumH = H / CellStep;
CellNumW = W / CellStep;
Overlap = 1;
BlockStep = 2;
BlockNumH = (CellNumH - Overlap) / (BlockStep - Overlap);
BlockNumW = (CellNumW - Overlap) / (BlockStep - Overlap);
Feature = zeros(BinNum,1);
Temp = zeros(BinNum,1);
for i = 1:BlockNumH
for j = 1:BlockNumW
TempFea = Hist((i - 1) * (BlockStep - Overlap) + 1:(i - 1) * (BlockStep - Overlap) + BlockStep,(j - 1) * (BlockStep - Overlap) + 1:(j - 1) * (BlockStep - Overlap) + BlockStep,:);
for w = 1:BlockStep
for l = 1:BlockStep
if i == 1 && j == 1 && w == 1 && l == 1
Feature(:) = TempFea(w,l,:);
else
Temp(:) = TempFea(w,l,:);
Feature = [Feature;Temp];
end
end
end
end
end
评论1
最新资源