File Exchange

image thumbnail

HOG descriptor for Matlab

version 1.1 (1.85 KB) by

Image descriptor based on Histogram of Oriented Gradients for gray-level images



View License

This code is simple and commented, what enables the adjust of the HOG parameters. This code was developed for the work: O. Ludwig, D. Delgado, V. Goncalves, and U. Nunes, 'Trainable Classifier-Fusion Schemes: An Application To Pedestrian Detection,' In: 12th International IEEE Conference On Intelligent Transportation Systems, 2009, St. Louis, 2009. V. 1. P. 432-437. In case of publication with this code, please cite the paper above.

Comments and Ratings (40)


azkya novia


Hi.. Very skilled that code.
Would u mind telling me what I should add for recognizing a face with ur code. I ran it and it works perfectly but when I give it a face, it just displays an image with noise and other image with the contorn.
Thanks in Avance.

Paula Marti


i need help to make the code for
combining Food Image Recognition by Using Bag-of-SURF Features and HOG Features

dhia jamaa

Oswaldo Ludwig

Oswaldo Ludwig (view profile)

Thanks, Hongmou!

It is a really nice code, Oswaldo.


I'm getting error with the code as
Attempted to access v_angles(1); index out of bounds because numel(v_angles)=0.

Error in HOG (line 44)
                if v_angles(k)<ang_lim

can I please know what is the error?

Hisham Sager

After taking the HOG of a whole image, and taking the HOG of a 32x32 section, comparing the same location from the whole image HOG and the 32x32 HOG they are not the same. Even in the areas in the middle of the image which are far from the borders.
What is the reason?

Akshay Gore

Akshay Gore (view profile)


Izem (view profile)

I think the code proposed by @Aaron is very helpful


I noticed a bug in the histogram computation. Gradient vectors whose angle is equal to pi are being omitted from the histogram because you check for angle < angle_lim instead of angle <= angle_lim.

I also re-wrote the histogram computation to optimize it in a way similar to what was suggested in an earlier comment.

% Compute the bin index for every angle.
binIndeces = ceil((v_angles + pi) / binSize);

% For each bin
for (bin = 1:B)
    % Get the magnitudes for all the pixels that belong in 'bin'.
    magnitudes = v_magnit(binIndeces == bin);
    % Add all of the magnitudes to 'bin'.
    H2(bin) = H2(bin) + sum(magnitudes);


@Oswaldo Ludwig sir, may i have your full paper coding ''Trainable Classifier-Fusion Schemes: An Application To Pedestrian Detection''

Oswaldo Ludwig

Oswaldo Ludwig (view profile)

Could you explain why it was adopted blockwise normalization? From the best of my knowledge, there is no mathematical foundation behind HOG descriptors, only good sense. This is my version, which was made available to be evaluated by the community. Go ahead, you should make the comparison with other algorithms in your case study (Science is not religion, feel free to doubt and inovate).

Lukas Tencer

Also you are doing normalization of the final vector, originally it supposed to be blockwise normalization, did you try that version? Why you decided for normalization only of final histogram.

Lukas Tencer

Should not be contribution of individual points to the histogram weighted according to gaussian centred in the middle of each cell?

Can anyone help I am runiing this code but finding error?
Thanks inadvance

??? Input argument "Im" is undefined.

Error in ==> HOG at 11
[L,C]=size(Im); % L num of lines ; C num of columns


sir,i run your code,i am getting the error

??? Input argument "Im" is undefined.

Error in ==> HOG at 11
[L,C]=size(Im); % L num of lines ; C num of columns

please help me to correct this.


Raul (view profile)


Alsam (view profile)


saif (view profile)

Can we use this code for object NOT human?

If I want to classify objects such as coca-cola and ketchup, can I do this directly with this code, I mean to use these features?

Oswaldo Ludwig

Oswaldo Ludwig (view profile)

Raul, the number of features depends on the number of cells and histograms bins, see Section II-A of this paper:[113]2009ITSC09-129-FI.pdf


Raul (view profile)

I would like to know why the size of the HOG vector is the same for each image with different sizes.


Prabhu (view profile)


Aaron (view profile)

replace lines 42-47 with code below for 20x speed-up

 idx = find(v_angles<ang_lim);
 v_angles(idx) = 100;
 H2(bin) = H2(bin) + sum(v_magnit(idx));

Oswaldo Ludwig

Oswaldo Ludwig (view profile)

An application for human detection (Windows only) can be found in
The program is based on HOG descriptors for Matlab arranged in SRM-cascade, according to the methodology described in: O. Ludwig; “ Study on Non-parametric Methods for Fast Pattern Recognition with Emphasis on Neural Networks and Cascade Classifiers;” PhD Thesis, University of Coimbra, Coimbra, 2012. New images can be included in the folder "images" with the name "ped_XX". The algorithm was trained by using 18x36 gray-scale images from the Daimler Pedestrian Classification dataset. Double-click in detector_pkg.exe to install the program, then it is possible to run detector.exe.


Swan (view profile)

Hi,what is this section for?
if C==1
in what case, C==1?

Caelyn Low


Aeris (view profile)

Hi, do you have any example showing how the descriptor is applied to an 640x480 image?
And i don't really understand how does 2*pi/B = 360/9 => 20 degrees per bin? As i see it is equal to 40 degrees.

Please correct me if i were wrong, thanks.


Tom (view profile)

Hello. Why in this case is use y filter hy=-hx' and not hy=hx'?

Thank you for your answer in advance.


David Lewis

Oswaldo Ludwig

Oswaldo Ludwig (view profile)

Dear Luis, you're right, a revised version was submitted.

Hello, I have found a error in your HOG descriptor program, the magnitude is calculated wrong. You multiply when you should sum.

Oswaldo Ludwig

Oswaldo Ludwig (view profile)

We have achieved good result in object detection, regarding detection speed and accuracy, by combining this descriptor with NN trained by MMGDX, see:



A problem in the calculation of gradient norm was corrected

MATLAB Release
MATLAB 7 (R14)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video