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.
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 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 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 at orientation degrees, inside the 40 to 60 degree bin pair. The upper weight is , so the 60 degree bin gets and the 40 degree bin gets the remaining ; the two votes sum back to . Suppose its block vector of 36 values has squared sum , hence norm , so the strong vote normalizes to . For the standard window, cells across and down give blocks, and 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)# -> 3780The 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 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 cells with 9 bins.
- Votes split between neighboring bins, so at degrees gives and .
- Blocks of cells normalize with L2-Hys for lighting robustness.
- A window yields exactly values scored by a linear SVM.
- Sliding windows plus non-maximum suppression turn scores into boxes.