AI Glossary / Computer vision and multimodal AI / Beginner
Template matching
Definition
Template matching searches for image regions that resemble a supplied example patch, called a template. A similarity or difference measure compares the template with candidate locations in a larger image. The resulting scores identify possible matches, but their meaning depends on the comparison method, threshold, and allowed changes in appearance.
What it means
The template is a concrete visual example, such as a symbol, a component, or a small printed mark. The basic task is to find where that example appears in another image.
This differs from general object recognition. A template of one screw head under one camera angle does not automatically describe every screw head under every viewpoint. Basic pixel-based matching is especially sensitive to changes in scale, rotation, lighting, and occlusion.
The score map records comparisons across positions. It can contain several plausible peaks, particularly for repeated patterns. Selecting the single best score answers which candidate is best among those examined; it does not prove that the object is actually present.
How it works
In a basic sliding-window method, the template is positioned at each valid location in the search image. A score compares its pixels with the corresponding image patch.
Squared-difference methods prefer smaller scores. Correlation-based methods commonly prefer larger scores, and normalized variants account for some changes in overall magnitude or contrast according to their formula. The method's direction and conventions must be checked before choosing a match.
For an image of width W and height H, and a template of width w and height h, unit-stride valid matching produces a score map of size (W − w + 1) by (H − h + 1). Additional processing can search several scales or rotations and suppress duplicate detections, but those capabilities are not automatic consequences of the basic method.
Three practical examples
1. A packaging line locates a printed registration mark in a controlled camera view. A restricted search region and stable lighting make the template comparison easier to interpret.
2. A document-processing tool finds repeated icons in scanned forms. It tests scans at different resolutions because the same icon can occupy different pixel dimensions.
3. A software testing tool looks for a known interface element in screenshots. It checks theme and display-scaling changes that can alter the element's appearance even when its function is unchanged.
Think of it this way
Think of moving a small tracing over a larger drawing to find where the lines line up. The analogy explains position search. It is limited because numerical matching depends on pixel values and a scoring formula; it does not inherently recognize the same object when its appearance changes substantially.
Worked example
Use a one-dimensional example with template T = [1, 2, 1]. The search signal is I = [0, 1, 2, 1, 0]. There are 5 − 3 + 1 = 3 valid positions.
At position zero, the candidate is [0, 1, 2]. Its sum of squared differences from T is (0−1)² + (1−2)² + (2−1)² = 3.
At position one, the candidate is [1, 2, 1], giving a score of zero. At position two, the candidate is [2, 1, 0], giving 1 + 1 + 1 = 3. The minimum selects position one.
Now add ten to every search value. The previously exact patch becomes [11, 12, 11], whose raw squared-difference score is 300. Its shape is unchanged, but the score is no longer zero.
This demonstrates why a threshold chosen for raw differences depends on brightness conditions. A suitable normalized measure may address some intensity changes, but it will not automatically solve rotation, scale, or missing content.
Illustrative case study
Fictional case study: Markwell Foods uses template matching to locate a small mark before inspecting label placement. The prototype works on development images, but a new camera exposure setting changes the match-score distribution.
The team records scores for both genuine marks and confusing background patches. It compares a normalized method with the original difference measure and restricts the search to the expected area. It also includes images where the mark is absent, blurred, or partially covered.
The system reports an unresolved case when no candidate satisfies the acceptance rule instead of always returning the best location. This fictional workflow illustrates threshold evaluation and rejection behavior, not a claimed defect-detection rate.
Common misconceptions and mistakes
Do not assume that the highest numerical value always represents the best match. Some methods minimize a difference, while others maximize similarity.
Do not infer certainty from a normalized score without validation. A correlation value is not automatically a calibrated probability that the target is present.
Do not ignore repeated patterns. Several locations can resemble the template, and nearby overlapping detections can refer to the same object. The application needs a rule for multiple matches, duplicates, and ambiguity.
Business use
Template matching is useful when visual appearance is constrained and an example patch captures the target well. It can be simpler to inspect and maintain than a learned recognition system for a narrow, stable task.
Evaluate realistic variation before deployment: scale, rotation, lighting, background, and image quality. Keep template versions tied to the products or interfaces they represent. Define both acceptance and rejection behavior. A system that always produces a location can appear decisive while silently matching an unrelated patch when the target is missing.
People also ask
Does template matching require training data?
Basic template matching can work directly from an example patch. Choosing robust templates and thresholds still requires representative testing.
Can it find rotated objects?
A basic fixed-orientation search does not provide rotation invariance. Additional rotated templates or a different method may be needed.
Can it find several copies?
Yes, by examining the score map and applying suitable thresholds and duplicate-suppression rules rather than selecting only one location.
Check your understanding
A template is absent, but the system still returns the location with the best score. Does that establish a successful detection?
Answer: No. It only identifies the best candidate examined. An independently validated acceptance rule is needed to distinguish matches from nonmatches.
Related terms
- Image registration
- Sliding window
- Cross-correlation
- normalized correlation
- nonmaximum suppression
- feature matching
Learn next
Compare squared differences and normalized correlation on a small image. Change brightness, scale, and rotation separately, then inspect the score map and test a case in which the template is completely absent.
More in Computer vision and multimodal AI
- Swendsen--Wang Algorithm
- Synthetic aperture - A synthetic aperture is an effective imaging aperture created by combining measurements taken from different positions.
- Teknomo - Fernandez algorithm - The Teknomo - Fernandez algorithm is a video-background estimation method that combines sampled frames using bitwise majority operations in a...
- Temporal aliasing - Temporal aliasing occurs when changes over time are sampled in a way that makes different underlying motions or fluctuations appear indistinguishable.
- Temporal redundancy - Temporal redundancy is repeated or predictable information across observations taken at different times.
- Texture - Texture is the visual pattern formed by variations in an image region, such as the grain of wood, a fabric weave, or a speckled surface.
Sources
OpenCV, Template Matching tutorial: https://docs.opencv.org/4.13.0/d4/dc6/tutorial_py_template_matching.html
The official tutorial supports sliding comparisons, score maps, and method conventions. The one-dimensional scores and fictional registration-mark workflow are original examples.
Previous: Template filling Next: Template recognition