Extract histogram of oriented gradients (HOG) features
extracted HOG features from a truecolor or grayscale input image,
features = extractHOGFeatures(
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.
HOG features extracted around specified point locations. The function
validPoints, which contains the
input point locations whose surrounding region is fully contained
I. Scale information associated with the
points is ignored.
[___] = extractHOGFeatures(___, uses
additional options specified by one or more Name,Value pair arguments,
using any of the preceding syntaxes.
Read the image of interest.
img = imread('cameraman.tif');
Extract HOG features.
[featureVector,hogVisualization] = extractHOGFeatures(img);
Plot HOG features over the original image.
figure; imshow(img); hold on; plot(hogVisualization);
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.
subplot(1,2,1); imshow(I1); subplot(1,2,2); plot(visualization);
Read in the image of interest.
I2 = imread('gantrycrane.png');
Detect and select the strongest corners in the image.
corners = detectFASTFeatures(rgb2gray(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.
figure; imshow(I2); hold on; plot(ptVis,'Color','green');
I— Input image
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.
points— Center location point
MSERRegionsobject | M-by-2 matrix of [x, y] coordinates
Center location point of a square neighborhood, specified as either a
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.
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
[2 2]sets the
BlockSizeto be a 2-by-2 square block.
'CellSize'— Size of HOG cell
[8 8](default) | 2-element vector
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.
'BlockSize'— Number of cells in block
[2 2](default) | 2-element vector
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.
'BlockOverlap'— Number of overlapping cells between adjacent blocks
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.
'NumBins'— Number of orientation histogram bins
9(default) | positive scalar
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.
'UseSignedOrientation'— Selection of orientation values
false(default) | logical scalar
Selection of orientation values, specified as a logical scalar.
When you set this property to
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.
features— Extracted HOG features
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.
|1-by-N vector||HOG feature length, N, is based on the image
size and the function parameter values.|
|P-by-Q matrix||P is the number of valid points whose surrounding
region is fully contained within the input image. You provide the |
The surrounding region is calculated as:
The feature vector length, Q, is calculated as:
The figure below shows an image with six cells.
If you set the
BlockSize to [
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
The HOG feature vector
is arranged by HOG blocks. The cell histogram, H(Cyx),
The figure below shows the HOG feature vector with a 1-by-1 cell overlap between blocks.
validPoints— Valid points
ORBPointsobject | M-by-2 matrix of [x,y] coordinates
Valid points associated with each
features descriptor vector output. This
output can be returned as either a
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
extracted descriptors are returned as the same type of object or matrix as
the input. The region must be fully contained within the image.
visualization— HOG feature visualization
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
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
You can use the following syntax to plot the HOG features:
 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.