最近在看HOG(Histogram of Gradient)方面的论文,由于blockproc一些优良的特性,因此可以考虑写一个相对简单的HOG特征参考实现。
首先,先编写一个blockproc函数句柄
function [img]=cell_angle_hist(Im, binnum,psize)
% Im: must be in the rage [0, 360]
side_x=ceil(sqrt(binnum));
angles_bin=zeros(side_x,side_x);
[rows,cols]=size(Im);
bin_size=360/binnum;
for y=1:rows
for x=1:cols
idx=ceil(Im(y,x)/bin_size);
angles_bin(idx)=angles_bin(idx)+1;
end
end
angles_bin=angles_bin./(rows*cols);
angles=linspace(0,360-1,binnum);
img=draw_angle(angles,angles_bin,psize);
function [patch]=draw_angle(angles,amps,psize)
patch=zeros(2*psize+1,2*psize+1);
base=patch;
base(psize+1,:)=1;
for i=1:length(angles)
angle=angles(i);
amp=amps(i);
p_angle=imrotate(base.*amp,angle,'crop');
patch=patch+p_angle;
end
patch(patch>=1)=1;
Im=imread('car1.bmp');
Dx=[-1 0 1];
Dy=Dx';
Im=double(Im);
Ix=imfilter(Im,Dx);
Iy=imfilter(Im,Dy);
Theta=atan2(Iy,(Ix+eps));
Theta=Theta/pi*180;
Theta=Theta+180;
fun = @(block_struct) cell_angle_hist(block_struct.data,16,16);
I2 = blockproc(Theta,[8 8],fun);
figure,imshow(I2,[])
I2 = blockproc(Theta,[16 16],fun);
figure,imshow(I2,[])
I2 = blockproc(Theta,[32 32],fun);
figure,imshow(I2,[])