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.
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” , 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.
Examples with Matlab
First, please download the image hog_image.jpg  that we are going to process.
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.
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.
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.
 Histograms of Oriented Gradients for Human Detection
 HOG Person Detector Tutorial
 HOG Tutorial from VLFeat