Histogram of Oriented Gradients

The histogram of oriented gradients (HOG) is a feature descriptor used in computer vision and image processing for the purpose of object detection.

The technique counts occurrences of gradient orientation in localized portions of an image. Local object appearance and shape within an image are described by the distribution of intensity gradients and edge directions. The image is divided into small connected regions called cells, and for the pixels within each cell, a histogram of gradient directions is compiled. The descriptor is the concatenation of these histograms.

Python Example

To download the code below, click here.

"""
histogram_of_oriented_gradients.py
uses the scikit-image libarary to process images
for more information see scikit-image.org
"""

# Import needed libraries.
import matplotlib.pyplot as plotlib
from skimage.feature import hog
from skimage import data, exposure

# Set parameters.
orientations = 8
cell_dimension = 16
cells_per_block = 1
block_normalization = 'L2-Hys'
test_image = data.coffee()
visualize_hog_image = True
multichannel_setting = True
feature_vector_setting=True
image_width = 8
image_height = 4
figure_size = (image_width, image_height)
image_rescale = 10
color_map = plotlib.cm.gray
axis_setting = "off"
property_sharing = True
test_image_title = "Test Image"
hog_image_title = "Histogram of Oriented Gradients"

# Instantiate a hog image using the test image.
hog_descriptor, hog_image = hog(test_image,
                                orientations=orientations,
                                pixels_per_cell=(cell_dimension, cell_dimension),
                                cells_per_block=(cells_per_block, cells_per_block),
                                block_norm=block_normalization,
                                feature_vector=feature_vector_setting,
                                visualize=visualize_hog_image,
                                multichannel=multichannel_setting)

# Print the hog descriptor array.
print("HOG Descriptor Feature Vector:")
print(hog_descriptor)

# Configure sub plots for the test_image and hog_image.
fig, (object_1, object_2) = plotlib.subplots(1, 2,
                                             figsize=figure_size,
                                             sharex=property_sharing,
                                             sharey=property_sharing)

# Plot the test_image.
object_1.axis(axis_setting)
object_1.imshow(test_image, cmap=color_map)
object_1.set_title(test_image_title)

# Plot the hog_image.
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, image_rescale))
object_2.axis(axis_setting)
object_2.imshow(hog_image_rescaled, cmap=color_map)
object_2.set_title(hog_image_title)

# Display the graphics.
plotlib.show()

Output is shown below:

HOG Descriptor Feature Vector:
[0.42718624 0.42718624 0.42718624 ... 0.04779452 0.04245345 0.1061488 ]