Skip to content
AI360XpertBeta

HOG Features and Classical Detection

HOG describes shape by counting edge directions in small cells, so a pedestrian reads the same in sun or shadow without any learned weights.

Gradients vote into 9 orientation bins per 8 by 8 cell, blocks of 2 by 2 cells normalize, and a 64 by 128 window yields 3780 values
Gradients vote into 9 orientation bins per 8 by 8 cell, blocks of 2 by 2 cells normalize, and a 64 by 128 window yields 3780 values

Why Does This Exist?

Before learned backbones, raw pixels were unusable for detection: they shift with lighting, shadows, and clothing color while the underlying shape stays put. Engineers needed a hand-built signature that kept silhouette structure and threw away the rest. The Histogram of Oriented Gradients, introduced by Dalal and Triggs in 2005 for human detection, does exactly that by recording only edge directions in small cells. Paired with a linear SVM over sliding windows, it became the standard pedestrian detector and the baseline every method in object detection had to beat, until two-stage detectors like Faster R-CNN replaced it.

Think of It Like This

Matchsticks tracing a bicycle

Imagine describing a bicycle using only matchsticks, with no color and no paint. Wheels become rings of matchsticks pointing in every direction, while the frame becomes straight runs of diagonally aligned sticks. The pattern of orientations alone reads as a bicycle whether the bike is blue or green, in sun or in shadow.

HOG is that matchstick tally per image patch. Each cell records which directions its edges point, and the stacked tallies form the signature. Where it stops: matchsticks capture outlines only, so a human printed on a billboard scores like a real pedestrian because the local geometry matches.

How It Actually Works

First compute horizontal and vertical gradients at every pixel, then magnitude and unsigned orientation from 0 to 180 degrees per pixel. Next divide the window into cells of 8×88 \times 8 pixels, where each cell votes its 64 pixels into a histogram of 9 orientation bins, each 20 degrees wide, weighted by magnitude with votes split between neighboring bins. Then group cells into overlapping blocks of 2×22 \times 2 cells with a stride of one cell, and normalize each block's 36 values with L2-Hys clipping, which factors out local lighting and shadow. Finally concatenate every normalized block of the detection window into one descriptor and score it with a linear SVM at each sliding position, merging overlaps with non-maximum suppression.

Worked example

A pixel casts magnitude 10.010.0 at orientation 53.053.0 degrees, inside the 40 to 60 degree bin pair. The upper weight is (53.0−40)/20=0.65(53.0 - 40) / 20 = 0.65, so the 60 degree bin gets 10.0×0.65=6.510.0 \times 0.65 = 6.5 and the 40 degree bin gets the remaining 3.53.5; the two votes sum back to 10.010.0. Suppose its block vector of 36 values has squared sum 156.25156.25, hence norm 156.25=12.5\sqrt{156.25} = 12.5, so the strong vote normalizes to 6.5/12.5=0.526.5 / 12.5 = 0.52. For the standard window, 64/8=864/8 = 8 cells across and 128/8=16128/8 = 16 down give (8−1)×(16−1)=7×15=105(8 - 1) \times (16 - 1) = 7 \times 15 = 105 blocks, and 105×4×9=3780105 \times 4 \times 9 = 3780 values in the final descriptor.

Code

The point of this snippet is the voting and counting, not the image I/O: it reproduces the 6.5 and 3.5 split plus the 3780 length the prose derived by hand, using only the standard library.

import math
mag, angle = 10.0, 53.0w_hi = (angle - 40.0) / 20.0print(round(mag * (1.0 - w_hi), 2), round(mag * w_hi, 2))# -> (3.5, 6.5)print(round(math.sqrt(156.25), 2), round(6.5 / 12.5, 2))# -> (12.5, 0.52)blocks = (8 - 1) * (16 - 1)print(blocks * 4 * 9)# -> 3780

The pair sums back to the input magnitude, the normalized vote matches the worked 0.52, and the block count confirms the descriptor length.

Watch Out For

Rigid window that misses other shapes

Classical HOG depends on a fixed aspect ratio window such as 64×12864 \times 128 for pedestrians. The symptom is missed detections on squat or wide objects no threshold recovers. Fix it by training separate window models per shape or moving to anchor-based detectors that tile many aspect ratios natively.

Confident detections on pictures of people

HOG is purely local, so a human printed on a billboard or a store mannequin scores like a real pedestrian. The symptom is high-confidence boxes on posters and reflections. Fix it with scene-level verification or a detector with wider context instead of lowering the SVM threshold.

The Quick Version

  • HOG ignores color and records only edge directions in 8×88 \times 8 cells with 9 bins.
  • Votes split between neighboring bins, so 10.010.0 at 5353 degrees gives 3.53.5 and 6.56.5.
  • Blocks of 2×22 \times 2 cells normalize with L2-Hys for lighting robustness.
  • A 64×12864 \times 128 window yields exactly 37803780 values scored by a linear SVM.
  • Sliding windows plus non-maximum suppression turn scores into boxes.