Skip to content

Template matching

Slide a patch across an image and score how well it agrees at each location.

Try it on your image ↓

VISUAL WALKTHROUGHMatching
REFERENCE INPUTSearch image

Find the location of a known 8 × 9 patch within this larger scene.

RESULTLocate the best match

The minimum-error response points back to the matching patch in the original image. Correlation-based methods use a different score convention.

STEP 03 / 03

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

YOUR IMAGE · REAL OPENCV

Experiment at pixel level

Open full lab ↗

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.

Input448 × 320
OutputWaiting for a result

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
Hover to inspect. Click to pin a pixel.
Input
Select a pixel

Output
Select a pixel

Sample models and licenses

When to use it

Find a known drawing, UI element or local translation when scale and orientation are stable.

How it works

  1. 01Place the template at each valid image position.
  2. 02Compute squared difference, correlation, or normalized correlation.
  3. 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.