看SURF结果把HOG看的太细了 都是学习 就记一下HOG的matlab实现 方便以后学习
function [gMag,gDir] = computeGradient(img)
gx = zeros(size(img),class(img));
gy = zeros(size(img),class(img));
gx(:,2:end-1) = conv2(img, [1 0 -1], 'valid');
gy(2:end-1,:) = conv2(img, [1;0;-1], 'valid');
% forward difference on borders
gx(:,1) = img(:,2) - img(:,1);
gx(:,end) = img(:,end) - img(:,end-1);
gy(1,:) = img(2,:) - img(1,:);
gy(end,:) = img(end,:) - img(end-1,:);
% return magnitude and direction
gMag = hypot(gx,gy);
gDir = atan2d(-gy,gx);
hypot是matlab自带的一个函数 看解释
%HYPOT Robust computation of the square root of the sum of squares
% C = HYPOT(A,B) returns SQRT(ABS(A).^2+ABS(B).^2) carefully computed to
% avoid underflow and overflow.
%
% Example:
% format short e
% a = 3*[1e300 1e-300]
% b = 4*[1e300 1e-300]
% c1 = sqrt(a.^2 + b.^2)
% c2 = hypot(a,b)
%
% x = 1.271161e308
% y = hypot(x,x)
%
% Class support for inputs A, B:
% float: double, single
%
% See also ABS, NORM, SQRT.
% Copyright 1984-2008 The MathWorks, Inc.
% $Revision: 1.1.6.2 $ $Date: 2008/03/13 17:32:00 $
% Built-in function.
然后ATAN2D(Y,X)也是
%ATAN2D Four quadrant inverse tangent, result in degrees.
% ATAN2D(Y,X) is the four quadrant arctangent of the elements
% of X and Y. -180 <= ATAN2D(Y,X) <= 180.
%
% See also ATAND, ATAN2.
% Copyright 1984-2011 The MathWorks, Inc.
% $Revision: 1.1.6.1 $ $Date: 2011/12/16 16:32:53 $
% Built-in function. 以上工作都是为了计算梯度,那HOG到底是什么呢,往下看