hog特征的matlab实现

matlab自带函数extractHOGFeatures可以很好地提取hog特征,但是不好进行改进,因此在网上找了人家用maltab实现的hog特征提取,但是性能稍差于自带函数,代码主要来自GitHub,具体地址为:https://github.com/chrisjmccormick/hog_matlab
针对hog特征提取部分,有稍微进行修改,代码如下:
测试用的代码如下
testgetHOGDescriptor.m

%% 参数设置
blocksize=3;  %一个block有2x2个cell
cellsize=32;   %一个cell的大小为8像素x8像素
imgsize=[256 256];
overlap=true;  %是否有重叠,重叠部分为50%,不可修改。
numbins=9;
%% 调用函数来传参
hog=sethog(blocksize,cellsize,imgsize,overlap,numbins);
% Compute the expected window size (with 1 pixel border on all sides).
% hog.winSize = [(hog.numVertCells * hog.cellSize + 2), ...
%                (hog.numHorizCells * hog.cellSize + 2)];
%            %加2是因为当时取梯度的时候去除了边缘,现在加回去
% fprintf('Getting the HOG descriptor for an example image...\n');

% Read in the pre-cropped (66 x 130) image.
%img = imread('./Images/Training/Positive/IMG_0009_x517_y326_w76_h177.png');
%% 读取maltab自带图片
img = imread('rice.png');
imshow(img);
img=imresize(img,hog.winSize);%缩放至这个大小,必须是hog.winSize,此时hog.winSize不等于imgsize
[m,n]=size(img);
%% 调用函数来计算单张图片的hog特征
% Compute the HOG descriptor for this image.
H = getHOGDescriptor(hog, img);%hog是个结构体,有很多属性

注意,该代码调用了两个函数,所以需要将这两个函数也放在同一个文件夹中
sethog.m

function hog=sethog(blocksize,cellsize,imgsize,overlap,numbins)
% The number of bins to use in the histograms.
hog.numBins = numbins;%原论文里面就是9个通道效果最好

% Cell size in pixels (the cells are square).%cell的边长为8像素
hog.cellSize = cellsize;%cell的大小为边长为8像素,cell是正方形的
%block的大小,边长为3个cell
hog.blockSize=blocksize;
hog.winSize=imgsize;%图片的大小
hog.overlap=overlap;%不重叠,重叠时重叠部分是50%

%将图片大小缩放至可以整除的范围
new_row = floor(hog.winSize(1)/hog.cellSize) * hog.cellSize;
new_col = floor(hog.winSize(2)/hog.cellSize) * hog.cellSize;
% new_img = imresize(img, [new_row new_col], 'bilinear');%以双线性插值来缩放
hog.winSize=[new_row+2 new_col+2];%更新hog.winSize
% The number of cells horizontally and vertically.
%cell的数量,一张图水平和竖直方向有多少个cell   130除以8=16.25(取整16)66除以8=8.25(取整8)
% hog.numHorizCells = 8;
% hog.numVertCells = 16;



hog.numHorizCells =(hog.winSize(2)-2)/hog.cellSize;
hog.numVertCells = (hog.winSize(1)-2)/hog.cellSize;
end

getHOGDescriptor.m

function H = getHOGDescriptor(hog, img)
% GETHOGDESCRIPTOR computes a HOG descriptor vector for the supplied image.
%   H = getHOGDescriptor(img)
%  
%   This function takes a 130 x 66 pixel gray scale image (128 x 64 with a 
%   1-pixel border for computing the gradients at the edges) and computes a 
%   HOG descriptor for the image, returning a 3,
  • 4
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值