Histograms of Oriented Gradients Tutorial with Matlab

Histograms of Oriented Gradients is a feature extraction method that can generate descriptors from images. This blog post aims at providing illustrative examples of HOG with Matlab, as well as discussing its interesting characteristics.

HOG Person Detector Tutorial
I have found a very nice and intuitive tutorial here. In this post, I will focus on the illustrative examples with Matlab.

Key Idea
The key idea of HOG is that, “local object appearance and shape can often be characterized rather well by the distribution of local intensity gradients or edge directions, even without precise knowledge of the corresponding gradient or edge positions” [1], meaning the distribution of gradients can represent the appearance of an image to some extent. The transformation from images to HOG achieves invariance to local geometric and photometric transformations by ignoring specific image details while remaining the distribution of gradients.

Software
Here I use the implementation of HOG from VLFeat. You can also find a tutorial of HOG using VLFeat here. Before we start, you need to have Matlab and VLFeat installed.

Examples with Matlab
First, please download the image hog_image.jpg [4] that we are going to process.

hog_image

The following code computes and visualizes the HOG features of the image.

[code language="matlab"]
% load image
im=imread('hog_image.jpg');
% specify cell size
cellSize = 4 ;
% compute HOG descriptor
hog = vl_hog(single(im), cellSize, 'verbose') ;
% generate a pictorial rendition of the features
imhog = vl_hog('render', hog, 'verbose') ;
% visualize the features
clf ; imagesc(imhog) ; colormap gray ;
[/code]

The following image represents the resulting descriptors from HOG. We can find that the HOG descriptors generally represents the appearance and shape of the image.

descriptor8

Granularity of Cell Size
One natural question that we can ask is, what will the descriptors look like when changing the granularities of the cell size. The following code computes HOG while varying the cell size from 5 to 60.

[code language="matlab"]
figure('Name','HOG with different granularity')
for i=1:12
    index = i;
    cellSize = 5*i;
    % compute HOG descriptor
    hog = vl_hog(single(im), cellSize, 'verbose') ;
    % generate a pictorial rendition of the features
    imhog = vl_hog('render', hog, 'verbose') ;
    % visualize the features
    subplot(3,4,i), subimage(imhog);
    title(5*i);
end
[/code]

The following figure shows the different descriptors generated from different granularities of cell size. We can find that, in fine granularity, more details are remained in the descriptors while the number of features are big and the descriptors may contain information that we are not interested in. On the other hand, in coarse granularity, more high-level information are remained with less number of features while important details could be lost. Therefore, a tradeoff between different levels of details, as well as a tradeoff between the number of features should be considered.

hog_granularity

Geometric and Photometric Invariance
Geometric invariance is achieved by quantization of orientations: if a local image is rotated, as long as the degree of rotation is within the bin size of histogram, it will make little difference to the out coming descriptors.
Photometric invariance is achieved by normalizing the gradients in overlapping blocks.

References
[1]  Histograms of Oriented Gradients for Human Detection
[2] HOG Person Detector Tutorial
[3] VLFeat
[4] HOG Tutorial from VLFeat

Leave a Reply

Your email address will not be published. Required fields are marked *