Main Content

extractHOGFeatures

Extract histogram of oriented gradients (HOG) features

Description

example

features= extractHOGFeatures(I)returns extracted HOG features from a truecolor or grayscale input image,I。The features are returned in a 1-by-Nvector, whereNis 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.

example

[features,validPoints] = extractHOGFeatures(I,points)returns HOG features extracted around specified point locations. The function also returnsvalidPoints,其中包含的输入点位置的年代urrounding region is fully contained withinI。Scale information associated with the points is ignored.

example

[___,visualization] = extractHOGFeatures(I,___)可以返回一个猪特征可视化,我们ing any of the preceding syntaxes. You can display this visualization usingplot(visualization).

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

Examples

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.

figure; imshow(img); holdon; plot(hogVisualization);

Figure contains an axes object. The axes object contains 3 objects of type image, line. One or more of the lines displays its values using only markers

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);

Figure contains 2 axes objects. Axes object 1 contains 2 objects of type line. One or more of the lines displays its values using only markers Axes object 2 contains an object of type image.

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.

figure; imshow(I2); holdon; plot(ptVis,'Color','green');

Figure contains an axes object. The axes object contains 6 objects of type image, line, rectangle. One or more of the lines displays its values using only markers

Input Arguments

collapse all

Input image, specified in eitherM-by-N-by-3 truecolor orM-by-N2-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 aBRISKPoints,SURFPoints,MSERRegions,ORBPointsorcornerPointsobject, or anM-by-2 matrix ofMnumber 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 theBlockSizeparameter. 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 asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis 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 encloseNamein quotes.

Example:'BlockSize',[2 2]sets theBlockSizeto 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 totrue, orientation values are evenly spaced in bins between -180 and 180 degrees. When you set this property tofalse, 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-Nvector or aP-by-Qmatrix. 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.

featuresoutput Description
1-by-Nvector HOG 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-Qmatrix Pis the number of valid points whose surrounding region is fully contained within the input image. You provide thepointsinput value for extracting point locations.
The surrounding region is calculated as:
CellSize。*BlockSize
The feature vector length,Q, is calculated as:
prod([NumBins,BlockSize]).

Example 1. Arrangement of Histograms in HOG Feature Vectors

The figure below shows an image with six cells.

If you set theBlockSizeto [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 theCellSizeproperty.

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 eachfeaturesdescriptor vector output. This output can be returned as either acornerPointsobject,BRISKPoints,SURFPointsobject,MSERRegionsobject,ORBPointsobject or anM-by-2 matrix of [x,y] coordinates. The function extractsMnumber 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 theplotmethod with thevisualizationoutput. See theExtract and Plot HOG Featuresexample.

HOG features are visualized using a grid of uniformly spacedroseplots. 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 timesNumBinspetals.

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, wherecolorValuerepresents the color as a 1-by-3 RGB vector, a short, or a long color name, described in theColor Valuetable.

More About

collapse all

Color Value

Color Name Short Name RGB Triplet Appearance
"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

References

[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
生成C和c++代码使用MATLAB®编码器™。

Version History

Introduced in R2013b