template matching
Imagine you cut out a small photo of a particular logo and then slide that cutout, like a magnifying glass, across a much larger picture. At every stop you ask a single question: how well does my cutout line up with the pixels underneath it right now? Template matching is exactly this. You hold a small reference image, called the template, and you place it at every possible position over a larger target image, computing a similarity score at each location. The position with the best score is your best guess for where the template appears.
The two classic scores are SSD (sum of squared differences), which adds up the squared pixel-by-pixel intensity gaps and is therefore smallest at the best match, and NCC (normalized cross-correlation), which measures the statistical correlation between the template and the underlying patch after subtracting each one's mean and dividing by its standard deviation. In notation: for a template T of width w and height h, and an image patch taken at offset (u,v), SSD is the sum over every template pixel (x,y) of (I(u+x, v+y) − T(x,y))², where I is the image. NCC instead recenters both the template and the patch to zero average brightness, rescales them to unit spread, and then takes their dot product; it ranges from −1 to +1, with +1 being a perfect match. The crucial advantage of NCC is invariance to linear brightness and contrast changes: if the whole patch is uniformly 20% brighter, NCC is unaffected, whereas raw SSD or unnormalized correlation would be badly misled.
Template matching is fast and reliable precisely when the target appears at the same scale, rotation, and lighting as the template, which is why it still powers industrial tasks like semiconductor wafer alignment, printed-circuit-board inspection, GUI test automation (find this icon on screen), and broadcast graphics. Its weakness is equally clear: a single rigid template cannot cope with scale change, rotation, perspective warp, non-rigid deformation, or partial occlusion. To handle those you would need a separate template per pose, which explodes combinatorially. This brittleness is exactly the gap that later methods set out to close: invariant keypoint descriptors such as SIFT, part-based deformable models, and ultimately learned convolutional features that recognize objects regardless of where and how they appear.
Finding a 32×32 logo in a 1920×1080 frame: slide the template across all ~2 million positions, compute NCC at each, and take the argmax. Thanks to illumination invariance, NCC ≈ 0.95 at the true location even if the broadcast frame is captured 20% brighter than the reference template.
Pitfall: unnormalized cross-correlation tends to fire on bright regions regardless of pattern, because a high-intensity flat patch can out-score the true match. Always use NCC (or SSD) when illumination varies, and remember template matching has no built-in scale or rotation invariance — you must search over a pyramid of scales and rotated templates yourself.