MATLAB implementation of HOG (Histogram of Oriented Gradients)

HOG Descriptor Matlab Implementation

HOG was used by Dalal and Triggs for human detection. As it is shown in Figure 1, the HOG method tiles the detector window with a dense grid of cells. Each cell contains a local histogram over orientation bins (Edge Orientation Histogram). At each pixel, the image gradient vector is calculated. The angle of the vector is used as a vote into the corresponding orientation bin and the vote is weighted by the gradient magnitude. Votes are accumulated over the pixels of each cell as shown in Figure 2. The cells are grouped into blocks and a robust normalization process (HOG normalization) is run on each block to provide strong illumination invariant. 

The normalized histograms of all blocks are concatenated to give the window-level visual descriptor vector for learning. Spatial and angular linear interpolation, and in some cases Gaussian windowing over the block, are used during voting to reduce aliasing . The blocks overlap spatially so that each cell appears several times with different normalizations, as this typically improves performance.

MATLAB implementation of HOG - Feature Extraction Process

Figure 1. HOG Feature Extraction

MATLAB implementation of HOG - Image Gradients and Spatial/Orientation Binning

Figure 2. Image Gradients and Spatial/Orientation Binning.

Histogram of Oriented Gradients (HOG) MATLAB Code Implementation

Here is the HOG feature extraction MATLAB code implementation:

findBlocksHOG is the main function that gets the input window and returns the calculated HOG. It extracts hog features

myGradient is the function used by findBlocksHOG function that calculates gradient and corrects gradients on edges


function [blocks]=findBlocksHOG (window, signed,cellSize,binSize,option,showWindow)

  • window : is the input (a sub window (ROI) of the image to process)
  • signed : choosing between signed or unsigned gradient (0 unsigned , 1 signed)
  • cellSize: the size of the cell(in pixel) involved in histogram calculation
  • binSize : the size of the orientation bin ( if gradient is signed -0 to 360, the bin size of 10 means : 0-35,36-71,....,328-359
  • option: define the output format that could be 'vector' , 'matrix'
  • showWindow : determine  'showWindow' for debug or 'noWindow'


function [Fx,Fy]=myGradient(I)

  • I : input Image
  • Fx: Gradient in X direction
  • Fy: Gradient in Y direction


  • Dalal, N., Triggs, B.: Histograms of Oriented Gradients for Human detection. In: CVPR 2005. ( pdf)
  • D. Lowe. Distinctive Image Features from Scale-Invariant Keypoints. IJCV, 60(2):91-110, 2004. ( pdf)