HOG特征向量的代码 源代码改
生活随笔
收集整理的這篇文章主要介紹了
HOG特征向量的代码 源代码改
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<pre name="code" class="cpp">function HOGFeature = ImgHOGFeature( imgPathName, cell_size, nblock,...,overlap, angle, bin_num)
% 計算輸入圖像的特征描述子
% imgPathName:圖片路徑
% cell_size: cell的長寬
% nblock: block的width、height包括的cell的個數(shù)
% overlap: block重疊的比例
% angle: 180\360
% bin_num: 方向bin的數(shù)目if nargin<2% default parametercell_size=8;nblock=2;overlap=0.5;angle=180;bin_num=9;
elseif nargin<6error('Input parameters are not enough.');
endImg = imread(imgPathName);
if size(Img,3) == 3% 簡化計算,直接轉(zhuǎn)換成灰度圖像G = rgb2gray(Img);
elseG = Img;
end[height, width] = size(G);% 計算x、y方向的梯度
hx = [-1,0,1];
hy = -hx';
grad_x = imfilter(double(G),hx);
grad_y = imfilter(double(G),hy);% 計算梯度的模長
grad_mag=sqrt(grad_x.^2+grad_y.^2);% 計算梯度的方向
index= grad_x==0;
grad_x(index)=1e-5;
YX=grad_y./grad_x;
if angle==180grad_angle= ((atan(YX)+(pi/2))*180)./pi;
elseif angle==360grad_angle= ((atan2(grad_y,grad_x)+pi).*180)./pi;
end% orient bin
bin_angle=angle/bin_num;
grad_orient=ceil(grad_angle./bin_angle);% 計算block的個數(shù)
block_size=cell_size*nblock;
skip_step=block_size*overlap;
x_step_num=floor((width-block_size)/skip_step+1);
y_step_num=floor((height-block_size)/skip_step+1);% 初始化hog特征描述子
feat_dim=bin_num*nblock^2;
HOGFeature=zeros(feat_dim,x_step_num*y_step_num);for k=1:y_step_numfor j=1:x_step_num% block的左上角坐標x_off = (j-1)*skip_step+1;y_off = (k-1)*skip_step+1;% 取得block的梯度大小和方向b_mag=grad_mag(y_off:y_off+block_size-1,x_off:x_off+block_size-1);b_orient=grad_orient(y_off:y_off+block_size-1,x_off:x_off+block_size-1);% 當(dāng)前block的hog直方圖currFeat = BinHOGFeature(b_mag, b_orient, cell_size,nblock, bin_num, false);HOGFeature(:, (k-1)*x_step_num+j) = currFeat;end
endend
function blockfeat = BinHOGFeature( b_mag,b_orient,cell_size,nblock,...bin_num, weight_vote)
% 計算1個block的hog
% weight_vote: 是否進行高斯加權(quán)投票% block的HOG直方圖
blockfeat=zeros(bin_num*nblock^2,1);% 高斯權(quán)重
gaussian_weight=fspecial('gaussian',cell_size*nblock,0.5*cell_size*nblock);% 分割block
for n=1:nblockfor m=1:nblock% cell的左上角坐標x_off = (m-1)*cell_size+1;y_off = (n-1)*cell_size+1;% cell的梯度大小和方向c_mag=b_mag(y_off:y_off+cell_size-1,x_off:x_off+cell_size-1);c_orient=b_orient(y_off:y_off+cell_size-1,x_off:x_off+cell_size-1);% cell的hog直方圖c_feat=zeros(bin_num,1);for i=1:bin_num% 是否進行高斯加權(quán) 投票if weight_vote==falsec_feat(i)=sum(c_mag(c_orient==i));elsec_feat(i)=sum(c_mag(c_orient==i).*gaussian_weight(c_orient==i));endend% 合并到block的HOG直方圖中count=(n-1)*nblock+m;blockfeat((count-1)*bin_num+1:count*bin_num,1)=c_feat;end
end% 歸一化 L2-norm
sump=sum(blockfeat.^2);
blockfeat = blockfeat./sqrt(sump+eps^2);
總結(jié)
以上是生活随笔為你收集整理的HOG特征向量的代码 源代码改的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HOG特征向量的代码
- 下一篇: Moravec算子