Skip to content
AI360Xpert

HOG Features and Classical Detection

The Histogram of Oriented Gradients (HOG) is a classical technique that detects objects by analyzing the distribution of edge directions in an image.

How HOG aggregates gradient vectors into cellular histograms to describe an object's shape.
How HOG aggregates gradient vectors into cellular histograms to describe an object's shape.

Why Does This Exist?

Before modern deep learning models like YOLO or Faster R-CNN dominated object detection, computer vision relied on "hand-crafted" features. To detect a pedestrian in an image, you could not simply feed raw pixels into a neural network. Raw pixels are too noisy and change dramatically based on lighting, shadows, and clothing color.

We needed a way to mathematically describe the shape and structure of an object in a way that ignored irrelevant details like color or minor lighting shifts. The Histogram of Oriented Gradients (HOG) was introduced in 2005 specifically for human detection. By focusing strictly on the edges and gradients in localized portions of an image, HOG successfully distilled the messy reality of a photograph into a clean, compact mathematical signature representing the object's silhouette. When paired with a Support Vector Machine (SVM) classifier, HOG became the industry standard for classical object detection.

Think of It Like This

Imagine trying to describe the shape of a bicycle to someone using only a collection of matchsticks. You cannot use color, and you cannot paint over the matchsticks.

To describe the wheels, you would arrange the matchsticks in a circle, with each matchstick pointing in a slightly different direction to form the curve. To describe the frame, you would place long lines of matchsticks pointing diagonally and horizontally. This is exactly what HOG does. It ignores the color and texture of the bicycle and instead breaks the image into small squares, recording only the dominant direction of the "matchsticks" (edges) in each square. The resulting pattern of oriented matchsticks is a robust signature that distinctly looks like a bicycle, regardless of whether the bike is red, blue, in bright sunlight, or in shadow.

How It Actually Works

The HOG descriptor works by evaluating local contrast changes (gradients) and building a histogram of these gradient directions for small regions of the image.

1. Gradient Calculation

The first step is to calculate the horizontal and vertical gradients of the image. For every pixel, the algorithm computes how sharply the image intensity changes in the x-direction and y-direction. This highlights the edges in the image. From these x and y gradients, we calculate the magnitude (how strong the edge is) and the orientation (which direction the edge is pointing) for every single pixel.

2. Cell Histograms

The image is then divided into small, interconnected regions called cells (typically 8×88 \times 8 pixels). For each cell, HOG creates a histogram of gradient orientations.

The histogram usually has 9 bins, representing angles from 0 to 180 degrees (e.g., 0°, 20°, 40°... 160°). Every pixel in the 8×88 \times 8 cell votes for one of these bins based on its gradient orientation. The "weight" of the vote is determined by the gradient magnitude. If a pixel represents a very strong edge pointing at 40 degrees, it adds a large value to the 40° bin. This step turns 64 raw pixels into a compact 9-value array representing the dominant edge directions in that tiny square.

3. Block Normalization

Lighting in an image is rarely uniform. One half of a pedestrian might be in bright sunlight, while the other half is in shadow. To make the descriptor robust to these lighting changes, the cell histograms must be normalized.

HOG groups multiple cells (typically 2×22 \times 2 cells) into larger blocks. It calculates the total energy (magnitude) across the entire block and normalizes the histograms of all four cells relative to this block-level energy. Because blocks overlap, a single cell's histogram is actually normalized multiple times against different neighboring contexts, making the final feature vector incredibly resilient to shadowing and illumination gradients.

4. The Final Feature Vector

The final step is to concatenate all the normalized histograms from all the overlapping blocks in the detection window into one massive 1D array. For a standard 64×12864 \times 128 pixel pedestrian detection window, this results in a single vector containing exactly 3,780 values. This vector is the HOG descriptor—a complete mathematical summary of the object's shape—which is then fed into a machine learning classifier (like a linear SVM) to determine if the object is a pedestrian or background noise.

Code

from skimage.feature import hogfrom skimage import exposureimport cv2
def extract_hog_features(image_path: str):    # Read image and convert to grayscale    img = cv2.imread(image_path)    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)        # Resize to a standard window size (e.g., for pedestrians)    resized = cv2.resize(gray, (64, 128))        # Calculate HOG features    # pixels_per_cell=(8, 8) creates the cellular histograms    # cells_per_block=(2, 2) handles the local contrast normalization    features, hog_image = hog(        resized,         orientations=9,         pixels_per_cell=(8, 8),        cells_per_block=(2, 2),         visualize=True    )        # Enhance the HOG image for visualization    hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))        # -> features is a 1D array of shape (3780,)    return features, hog_image_rescaled

Watch Out For

Rigid Window Requirements

Classical HOG depends on a fixed aspect ratio detection window (like the 64×12864 \times 128 window used for pedestrians). If you want to detect objects of vastly different shapes or aspect ratios, you have to train multiple different SVMs on different window sizes, making the pipeline rigid and slow compared to modern anchor-based deep learning methods.

Lack of Contextual Understanding

HOG is highly localized. It understands that a window contains the shape of a human, but it has no contextual understanding of the scene. It might detect a human shape printed on a billboard or a mannequin in a store window with the same confidence as a real pedestrian, because the local edge geometries are identical.

The Quick Version

  • HOG (Histogram of Oriented Gradients) is a classical feature extraction technique used primarily for object detection.
  • It completely ignores color, focusing entirely on the direction and strength of edges (gradients) to represent an object's shape.
  • The image is divided into small cells, and a histogram of edge directions is tallied for each cell.
  • Cells are normalized in overlapping blocks to make the detection robust against lighting and shadow changes.
  • The resulting feature vector is typically fed into an SVM to classify whether a specific object (like a pedestrian) is present.