• 大小: 1KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-04-20
  • 语言: Matlab
  • 标签: MATLAB  HOG  SVM  

资源简介

根据Dalal提出的HOG特征算法编写,代码简单,注释清楚,根据所处理图像的名称路径修改后可生成36*105的特征向量。

资源截图

代码片段和文件信息

% imgPathName: 图片路径及名称
% cell_size: cell的长宽
% nblock: block的width及height包括的cell个数
% overlap: block重叠比例
% angle: 180/360
% bin_num: 方向bin的数目
% weight_vote: 高斯加权投票

function HOG = HOG(imgPathName cell_size nblock...                        % 计算图像的HOG特征向量
               overlap angle bin_num)

if nargin<2
    cell_size=8;                                                              % 默认参数
    nblock=2;
    overlap=0.5;
    angle=180;
    bin_num=9;
elseif nargin<6
    error(‘Input parameters are not enough.‘);
end
 
Img = imread(imgPathName);
if size(Img3) == 3
    G = rgb2gray(Img);                                                        % 简化计算,直接转换成灰度图像
else
    G = Img;
end
 
[height width] = size(G);

hx = [-101];                                                                % 计算x、y方向的梯度
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==180
    grad_angle= ((atan(YX)+(pi/2))*180)./pi;
elseif angle==360
    grad_angle= ((atan2(grad_ygrad_x)+pi).*180)./pi;
end
 
bin_angle=angle/bin_num;                                                      % 标定方向
grad_orient=ceil(grad_angle./bin_angle);
 
block_size=cell_size*nblock;                                                  % 计算block个数
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);
 
feat_dim=bin_num*nblock^2;                                                    % 初始化HOG特征描述子
HOG=zeros(feat_dimx_step_num*y_step_num);
 
for k=1:y_step_num
    for j=1:x_step_num
        x_off = (j-1)*skip_step+1;                                            % block左上角坐标
        y_off = (k-1)*skip_step+1;
 
        b_mag=grad_mag(y_off:y_off+block_size-1x_off:x_off+block_size-1);    % 获取block梯度大小与方向
        b_orient=grad_orient(y_off:y_off+block_size-1x_off:x_off+block_size-1);
 
        currFeat = BlkHOG(b_mag b_orient cell_sizenblock bin_num false); % 当前block的HOG
        HOG(: (k-1)*x_step_num+j) = currFeat; 
    end
end

function BlkHOG = BlkHOG( b_magb_orientcell_sizenblock...                 % 计算单个block的HOG特征向量
                  bin_num weight_vote)

BlkHOG=zeros(bin_num*nblock^21);

gaussian_weight=fspecial(‘gaussian‘cell_size*nblock0.5*cell_size*nblock);   % 高斯权重

for n=1:nblock                                                                % 分割block
    for m=1:nblock
        x_off = (m-1)*cell_size+1;                                            % cell左上角坐标
        y_off = (n-1)*cell_size+1;

        c_mag=b_mag(y_off:y_off+cell_size-1x_off:x_off+cell_size-1);         % cell梯度大小与方向
        c_orient=b_orient(y_off:y_off+cell_size-1x_off:x_off+cell_size-1);
 
        c_feat=zeros(bi

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        3828  2013-03-19 17:05  HOG.m

评论

共有 条评论