matlab Hog特征提取浅析
2016-12-03 19:14
603 查看
最近,利用自己的业余时间学习了Hog特征的提取方法,并编写了一个简单的matlab程序。限于自身的程序水平,有很多地方写的不是很好,请大家多多指正,我也会逐步完善代码,其实,这又何尝不是在完善我自己呢?
相关知识:http://blog.csdn.net/liulina603/article/details/8291093
close all;clear all;clc;
SrcImg = imread('E:\HOG\MFile\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
相关知识:http://blog.csdn.net/liulina603/article/details/8291093
close all;clear all;clc;
SrcImg = imread('E:\HOG\MFile\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
相关文章推荐
- Matlab 提取Hog特征方法详细解析
- HOG特征提取matlab代码
- matlab 提取HOG特征
- HOG特征提取matlab代码
- HOG特征提取 matlab
- 纹理特征提取 及LBP纹理特征matlab实现
- 目标检测的图像特征提取之(一)HOG特征
- 目标检测的图像特征提取之(一)HOG特征
- 图像处理之HOG特征提取
- 特征提取(Feature Detection)之——HOG (Histogram of Oriented Gradient)特征
- 目标检測的图像特征提取之(一)HOG特征
- 目标检测的图像特征提取之(三)HOG特征
- 图像特征提取三大法宝:HOG特征,LBP特征,Haar特征
- matlab 批量提取CNN特征
- 图像特征提取之HOG
- 目标检測的图像特征提取之(一)HOG特征
- 图像特征提取之(一)HOG特征
- matlab实现hog特征
- 图像特征提取三大法宝:HOG特征,LBP特征,Haar特征
- 目标检测的图像特征提取之(一)HOG特征