Main Content


Extract histogram of oriented gradients (HOG) features



features = extractHOGFeatures(I) returns extracted HOG features from a truecolor or grayscale input image, I. The features are returned in a 1-by-N vector, where N is the HOG feature length. The returned features encode local shape information from regions within an image. You can use this information for many tasks including classification, detection, and tracking.


[features,validPoints] = extractHOGFeatures(I,points) returns HOG features extracted around specified point locations. The function also returns validPoints, which contains the input point locations whose surrounding region is fully contained within I. Scale information associated with the points is ignored.


[___, visualization] = extractHOGFeatures(I,___) optionally returns a HOG feature visualization, using any of the preceding syntaxes. You can display this visualization using plot(visualization).

[___] = extractHOGFeatures(___,Name,Value) uses additional options specified by one or more Name,Value pair arguments, using any of the preceding syntaxes.


collapse all

Read the image of interest.

img = imread('cameraman.tif');

Extract HOG features.

[featureVector,hogVisualization] = extractHOGFeatures(img);

Plot HOG features over the original image.

hold on;

Read the image of interest.

I1 = imread('gantrycrane.png');

Extract HOG features.

[hog1,visualization] = extractHOGFeatures(I1,'CellSize',[32 32]);

Display the original image and the HOG features.


Read in the image of interest.

I2 = imread('gantrycrane.png');

Detect and select the strongest corners in the image.

corners   = detectFASTFeatures(im2gray(I2));
strongest = selectStrongest(corners,3);

Extract HOG features.

[hog2,validPoints,ptVis] = extractHOGFeatures(I2,strongest);

Display the original image with an overlay of HOG features around the strongest corners.

hold on;

Input Arguments

collapse all

Input image, specified in either M-by-N-by-3 truecolor or M-by-N 2-D grayscale. The input image must be a real, nonsparse value. If you have tightly cropped images, you may lose shape information that the HOG function can encode. You can avoid losing this information by including an extra margin of pixels around the patch that contains background pixels.

Data Types: single | double | int16 | uint8 | uint16 | logical

Center location point of a square neighborhood, specified as either a BRISKPoints, SURFPoints, MSERRegions, ORBPoints or cornerPoints object, or an M-by-2 matrix of M number of [x, y] coordinates. The function extracts descriptors from the neighborhoods that are fully contained within the image boundary. You can set the size of the neighborhood with the BlockSize parameter. Only neighborhoods fully contained within the image are used to determine the valid output points. The function ignores scale information associated with these points.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'BlockSize',[2 2] sets the BlockSize to be a 2-by-2 square block.

Size of HOG cell, specified in pixels as a 2-element vector. To capture large-scale spatial information, increase the cell size. When you increase the cell size, you may lose small-scale detail.

Number of cells in a block, specified as a 2-element vector. A large block size value reduces the ability to suppress local illumination changes. Because of the number of pixels in a large block, these changes may get lost with averaging. Reducing the block size helps to capture the significance of local pixels. Smaller block size can help suppress illumination changes of HOG features.

Number of overlapping cells between adjacent blocks, specified as a 2-element vector. To ensure adequate contrast normalization, select an overlap of at least half the block size. Large overlap values can capture more information, but they produce larger feature vector size. This property applies only when you are extracting HOG features from regions and not from point locations. When you are extracting HOG features around a point location, only one block is used, and thus, no overlap occurs.

Number of orientation histogram bins, specified as positive scalar. To encode finer orientation details, increase the number of bins. Increasing this value increases the size of the feature vector, which requires more time to process.

Selection of orientation values, specified as a logical scalar. When you set this property to true, orientation values are evenly spaced in bins between -180 and 180 degrees. When you set this property to false, they are evenly spaced from 0 through 180. In this case, values of theta that are less than 0 are placed into a theta + 180 value bin. Using signed orientation can help differentiate light-to-dark versus dark-to-light transitions within an image region.

Output Arguments

collapse all

Extracted HOG features, returned as either a 1-by-N vector or a P-by-Q matrix. The features encode local shape information from regions or from point locations within an image. You can use this information for many tasks including classification, detection, and tracking.

features outputDescription
1-by-N vectorHOG feature length, N, is based on the image size and the function parameter values.
N = prod([BlocksPerImage, BlockSize, NumBins])
BlocksPerImage = floor((size(I)./CellSizeBlockSize)./(BlockSizeBlockOverlap) + 1)
P-by-Q matrixP is the number of valid points whose surrounding region is fully contained within the input image. You provide the points input value for extracting point locations.
The surrounding region is calculated as:
The feature vector length, Q, is calculated as:

Example 1. Arrangement of Histograms in HOG Feature Vectors

The figure below shows an image with six cells.

If you set the BlockSize to [2 2], it would make the size of each HOG block, 2-by-2 cells. The size of the cells are in pixels. You can set it with the CellSize property.

The HOG feature vector is arranged by HOG blocks. The cell histogram, H(Cyx), is 1-by-NumBins.

The figure below shows the HOG feature vector with a 1-by-1 cell overlap between blocks.

Valid points associated with each features descriptor vector output. This output can be returned as either a cornerPoints object, BRISKPoints, SURFPoints object, MSERRegions object, ORBPoints object or an M-by-2 matrix of [x,y] coordinates. The function extracts M number of descriptors from valid interest points in a region of size equal to [CellSize.*BlockSize]. The extracted descriptors are returned as the same type of object or matrix as the input. The region must be fully contained within the image.

HOG feature visualization, returned as an object. The function outputs this optional argument to visualize the extracted HOG features. You can use the plot method with the visualization output. See the Extract and Plot HOG Features example.

HOG features are visualized using a grid of uniformly spaced rose plots. The cell size and the size of the image determines the grid dimensions. Each rose plot shows the distribution of gradient orientations within a HOG cell. The length of each petal of the rose plot is scaled to indicate the contribution each orientation makes within the cell histogram. The plot displays the edge directions, which are normal to the gradient directions. Viewing the plot with the edge directions allows you to better understand the shape and contours encoded by HOG. Each rose plot displays two times NumBins petals.

You can use the following syntax to plot the HOG features:

plot(visualization) plots the HOG features as an array of rose plots.
plot(visualization,AX) plots HOG features into the axes AX.
plot(___,'Color',colorValue) Specifies the color used to plot HOG features, where colorValue represents the color as a 1-by-3 RGB vector, a short, or a long color name, described in the Color Value table.

More About

collapse all

Color Value

Color NameShort NameRGB TripletAppearance
"red""r"[1 0 0]

Sample of the color red

"green""g"[0 1 0]

Sample of the color green

"blue""b"[0 0 1]

Sample of the color blue

"cyan" "c"[0 1 1]

Sample of the color cyan

"magenta""m"[1 0 1]

Sample of the color magenta

"yellow""y"[1 1 0]

Sample of the color yellow

"black""k"[0 0 0]

Sample of the color black

"white""w"[1 1 1]

Sample of the color white


[1] Dalal, N. and B. Triggs. "Histograms of Oriented Gradients for Human Detection", IEEE Computer Society Conference on Computer Vision and Pattern Recognition, Vol. 1 (June 2005), pp. 886–893.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2013b