HOG特征向量的代码
生活随笔
收集整理的這篇文章主要介紹了
HOG特征向量的代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
clear all; close all; clc;img=double(rgb2gray(imread('E:/6.jpg')));
imshow(img,[]);
[m n]=size(img);img=sqrt(img); %伽馬校正%下面是求邊緣
fy=[-1 0 1]; %定義豎直模板
fx=fy'; %定義水平模板
Iy=imfilter(img,fy,'replicate'); %豎直邊緣
Ix=imfilter(img,fx,'replicate'); %水平邊緣
Ied=sqrt(Ix.^2+Iy.^2); %邊緣強度
Iphase=Iy./Ix; %邊緣斜率,有些為inf,-inf,nan,其中nan需要再處理一下%下面是求cell
step=16; %step*step個像素作為一個單元
orient=9; %方向直方圖的方向個數
jiao=360/orient; %每個方向包含的角度數
Cell=cell(1,1); %所有的角度直方圖,cell是可以動態增加的,所以先設了一個
ii=1;
jj=1;
for i=1:step:m %如果處理的m/step不是整數,最好是i=1:step:m-stepii=1;for j=1:step:n %注釋同上tmpx=Ix(i:i+step-1,j:j+step-1);tmped=Ied(i:i+step-1,j:j+step-1);tmped=tmped/sum(sum(tmped)); %局部邊緣強度歸一化tmpphase=Iphase(i:i+step-1,j:j+step-1);Hist=zeros(1,orient); %當前step*step像素塊統計角度直方圖,就是cellfor p=1:stepfor q=1:stepif isnan(tmpphase(p,q))==1 %0/0會得到nan,如果像素是nan,重設為0tmpphase(p,q)=0;endang=atan(tmpphase(p,q)); %atan求的是[-90 90]度之間ang=mod(ang*180/pi,360); %全部變正,-90變270if tmpx(p,q)<0 %根據x方向確定真正的角度if ang<90 %如果是第一象限ang=ang+180; %移到第三象限endif ang>270 %如果是第四象限ang=ang-180; %移到第二象限endendang=ang+0.0000001; %防止ang為0Hist(ceil(ang/jiao))=Hist(ceil(ang/jiao))+tmped(p,q); %ceil向上取整,使用邊緣強度加權endendHist=Hist/sum(Hist); %方向直方圖歸一化Cell{ii,jj}=Hist; %放入Cell中ii=ii+1; %針對Cell的y坐標循環變量endjj=jj+1; %針對Cell的x坐標循環變量
end%下面是求feature,2*2個cell合成一個block,沒有顯式的求block
[m n]=size(Cell);
feature=cell(1,(m-1)*(n-1));
for i=1:m-1for j=1:n-1 f=[];f=[f Cell{i,j}(:)' Cell{i,j+1}(:)' Cell{i+1,j}(:)' Cell{i+1,j+1}(:)'];feature{(i-1)*(n-1)+j}=f;end
end%到此結束,feature即為所求
%下面是為了顯示而寫的
l=length(feature);
f=[];
for i=1:lf=[f;feature{i}(:)'];
end
figure
mesh(f)
總結
以上是生活随笔為你收集整理的HOG特征向量的代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Given an integer, wr
- 下一篇: HOG特征向量的代码 源代码改