img=imresize(img,[2^8,2^8]);%??????
img_size=size(img);
img=rgb2gray(img);
imshow(img);
img=((double(img)/255).^0.5)*255;%?????gamma??
figure,imshow(uint8(img));
x_filter=[0,0,0;-1,0,1;0,0,0];
y_filter=[0,-1,0;0,0,0;0,1,0];
x_grad=conv2(img,x_filter,'same');
y_grad=conv2(img,y_filter,'same');
%figure,imshow(uint8(x_grad));title('x-grad');
%figure,imshow(uint8(y_grad));title('y-grad');
grad_magntd=(x_grad.^2+y_grad.^2).^0.5;
grad_theta=atan(y_grad./x_grad)/pi*180;
grad_theta(grad_theta<0)=grad_theta(grad_theta<0)+180;%??0-180?
grad_theta(x_grad==0)=90;%x=0???NaN->90
figure,imshow(uint8(grad_magntd));title('grad-magnitude');
figure,imshow(uint8(grad_theta));title('grad-theta');
cell_size=2^3;
histograms_cell_size=[img_size(1)/cell_size,img_size(2)/cell_size];
histograms_cell=cell(histograms_cell_size);
grad_theta_bins=zeros(size(grad_theta));
for num=1:9
grad_theta_bins(grad_theta>=(num-1)*20&grad_theta<num*20)=num;
end
histograms_tem=zeros(1,9);
cell_resize=[cell_size*ones(1,histograms_cell_size(1));cell_size*ones(1,histograms_cell_size(2))];
theta_bins_cells=mat2cell(grad_theta_bins,cell_resize(1,:),cell_resize(2,:));%????cell
magntd_cells=mat2cell(grad_magntd,cell_resize(1,:),cell_resize(2,:));
for num1=1:histograms_cell_size(1)
for num2=1:histograms_cell_size(2)
theta_bins_tem=theta_bins_cells{num1,num2};%??????cell
magtd_tem=magntd_cells{num1,num2};
histograms_tem(theta_bins_tem)=histograms_tem(theta_bins_tem)+magtd_tem;
histograms_cell{num1,num2}=histograms_tem;
histograms_tem=zeros(1,9);
end
end
%% cell????
bim1 = zeros(cell_size);
bim1(:,round(cell_size/2):round(cell_size/2)+1) = 1;
bim = zeros([size(bim1) 9]);
bim(:,:,1) = bim1;
for i = 2:9
bim(:,:,i) = imrotate(bim1, -(i-1)*20, 'crop');
end
w=zeros(histograms_cell_size(1),histograms_cell_size(2),9);
for i=1:histograms_cell_size(1)
for j=1:histograms_cell_size(2)
w(i,j,:)=histograms_cell{i,j};
end
end
im = zeros(cell_size*histograms_cell_size(1), cell_size*histograms_cell_size(2));
for i = 1:histograms_cell_size(1)
iis = (i-1)*cell_size+1:i*cell_size;
for j = 1:histograms_cell_size(2)
jjs = (j-1)*cell_size+1:j*cell_size;
for k = 1:9
im(iis,jjs) = im(iis,jjs) + bim(:,:,k) * w(i,j,k);
end
end
end
im=round(im);
figure,imshow(uint8(im));title('HOG feature visualization');
%% block&final descript
block_size_cell=2;
stride_cell=1;
block_size=[(histograms_cell_size(1)-block_size_cell)/stride_cell+1,...
(histograms_cell_size(2)-block_size_cell)/stride_cell+1];
block_depict=cell(block_size);%???normalization
block_depict_norm=cell(block_size);%??normalization->normalization?????????
epsln=10^(-5);
for ii=1:block_size(1)
for jj=1:block_size(2)
block_tem=[histograms_cell{ii,jj},histograms_cell{ii,jj+1},...
histograms_cell{ii+1,jj},histograms_cell{ii+1,jj+1}];
squr_sum_root=(sum(block_tem.*block_tem)+epsln^2)^0.5;
block_tem_norm=block_tem/squr_sum_root; %L2-norm
block_depict{ii,jj}=block_tem;
block_depict_norm{ii,jj}=block_tem_norm;
end
end
block_depict2=block_depict';
block_depict_norm2=block_depict_norm';
final_descripter=[block_depict2{:,:}]; %??featVector
final_descripter_norm=[block_depict_norm2{:,:}]; %normalize???featVector
%% block????
bim1 = zeros(cell_size*block_size_cell);
bim1(:,round(cell_size*block_size_cell/2):round(cell_size*block_size_cell/2)+1) = 1;
bim = zeros([size(bim1) 9]);
bim(:,:,1) = bim1;
for i = 2:9
bim(:,:,i) = imrotate(bim1, -(i-1)*20, 'crop');
end
im2 = zeros(cell_size*histograms_cell_size(1), cell_size*histograms_cell_size(2));
for i = 1:block_size(1)
iis = stride_cell*(i-1)*cell_size+1:(stride_cell*(i-1)+2)*cell_size;
for j = 1:block_size(2)
jjs = stride_cell*(j-1)*cell_size+1:(stride_cell*(j-1)+2)*cell_size;
block_cur=block_depict_norm{i,j};
for k = 1:9
w_cur=sum(block_cur(k:9:end));
im2(iis,jjs) = im2(iis,jjs) + bim(:,:,k)*w_cur;
end
end
end
im2=round(im2);
im2=im2*(255/max(max(im2)));
figure,imshow(uint8(im2));title('HOG-block feature visualization');
评论0