The Histogram of Oriented Gradients, commonly referred to as HOG, is a feature extraction technique used in computer vision and image processing. It has found widespread applications in object detection, image recognition.
Feature extraction plays a crucial role in many computer vision tasks, where the goal is to represent complex visual data in a more meaningful and compact form. HOG achieves this by focusing on the distribution of gradient orientations within an image. The core idea behind HOG is to capture local intensity gradients and their orientations, which are essential for characterizing object shapes and structures.
The process of computing the Histogram of Oriented Gradients involves several steps:
The input image is preprocessed to improve its robustness against lighting variations and noise. Common preprocessing steps include converting the image to grayscale, normalizing pixel intensities, and applying contrast normalization.
HOG calculates the gradient magnitudes and orientations of image pixels. This step helps in identifying edges and texture boundaries.
The image is divided into small, overlapping cells. Typically, each cell covers a region of 8x8 pixels.
For each cell, a histogram of gradient orientations is computed. The orientations are quantized into bins, and the histogram represents the distribution of gradient orientations within the cell.
Cells are grouped into larger blocks (usually consisting of 2x2 or 3x3 cells). Normalization is applied within each block to enhance the algorithm’s robustness to changes in lighting and contrast.
The normalized histograms from all blocks are concatenated to form the final HOG descriptor for the image. This descriptor captures the spatial distribution of gradients and their orientations.
Let’s take an example to compute HOG on an image using the skimage.feature.hog()
below:
import matplotlib.pyplot as pltfrom skimage.feature import hogfrom skimage import data, exposureimport skimage.io as skioimage = skio.imread("starfish.png")fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),cells_per_block=(1, 1), visualize=True, channel_axis=-1)# Rescale histogram for better displayhog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)ax1.axis('off')ax1.imshow(image, cmap=plt.cm.gray)ax1.set_title('Input image')ax2.axis('off')ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)ax2.set_title('Histogram of Oriented Gradients')plt.show()
Lines 1–4: We import required modules to compute HOG on an image.
Line 5: The skio.imread()
function is used to read the image named "starfish.png"
and store it in the variable image
.
Lines 8–9: We calculate the Histogram of Oriented Gradients (HOG) features of the image using the hog()
function.
fd
: This variable will store the HOG feature descriptors.
hog_image
: This variable will store the HOG visualization image.
Parameters passed to hog()
:
image
: The input image on which HOG features are calculated.orientations = 8
: Number of bins in the histogram of oriented gradients.pixels_per_cell=(16, 16)
: Size of each cell in pixels.cells_per_block=(1, 1)
: Number of cells in each block.visualize=True
: Generates a visualization of the HOG image.channel_axis=-1
: Specifies that the function operates on the last dimension (color channels) of the image.Line 12: We rescale the intensity values of the HOG visualization image using exposure.rescale_intensity()
.
The hog_image is rescaled to the range (0, 10)
to enhance its visibility.
Lines 14–21: We plot the original image along with the histgoram gradient of that image using matplotlib
library.
Free Resources