Template matching
Slide a patch across an image and score how well it agrees at each location.
Find the location of a known 8 × 9 patch within this larger scene.
The template includes an object corner and surrounding background, not just a uniform interior.
Each response compares all template pixels with one candidate patch. This teaching example computes mean squared difference.
The minimum-error response points back to the matching patch in the original image. Correlation-based methods use a different score convention.
Find the best response and optionally refine the peak.
Computed teaching example on a 24 × 16 image. Small kernels and simplified settings keep each change visible; use the image laboratory for native OpenCV.
Try it on an image
Experiment at pixel level
Search the input for a crop of the second image. Without an upload, the template is cropped from the input itself.
The engine loads on your first run. Your images stay in this browser.
Scroll over either image to zoom at the pointer. Use the scrollbars to pan both views over the same relative area. Zoom is relative to the input; pixel coordinates belong to each image. Warps can change scene correspondence.
Pixel inspector RGBA · native values · matched scale · 9 × 9 output pixels
Select a pixel
Select a pixel
When to use it
Find a known drawing, UI element or local translation when scale and orientation are stable.
How it works
- 01Place the template at each valid image position.
- 02Compute squared difference, correlation, or normalized correlation.
- 03Find the best response and optionally refine the peak.
SQDIFF(x,y) = Σ (image patch − template)²
What to tune
TM_SQDIFF methods prefer minima; correlation methods prefer maxima. Mask support and normalization depend on the chosen method.
Where it breaks down
The template is not automatically rotation or scale invariant. Flat templates, repeated texture and partial occlusion can make the peak ambiguous.
TypeScript API
Open an entry for its exact overloads, parameter descriptions, result ownership and pinned upstream source.
All of these calls execute on the CPU. Native objects need explicit disposal. See matrices and ownership and build compatibility.
Related methods
Phase correlation
Estimate translation from the phase relationship between two images in the frequency domain.
ECC alignment
Refine a warp by maximizing enhanced correlation between an image and a template.
Brute-force descriptor matching
Compare each query descriptor with every training descriptor using a chosen distance.
FLANN nearest neighbours
Organize descriptors so nearest-neighbour queries can avoid an exhaustive scan.