Template Matching

Normalized Cross-Correlation

When comparing correlation responses across different filters or image patches, raw correlation values depend on the scale (amplitude) of both the filter and the local image patch. Normalized correlation addresses this by:

  1. Normalizing the filter so its standard deviation equals 1.

  2. At each position, normalizing the underlying image patch to unit standard deviation before computing the dot product.

This makes the correlation coefficient independent of local contrast and filter scale, producing values in [-1, 1].

1D Correlation for Matching

Given a 1D signal and a template (sub-signal), the normalized cross-correlation peaks at the position where the template best matches the signal. The maximum occurs because:

  • Where the template is positive, the signal is positive → positive product

  • Where the template is negative, the signal is negative → positive product

  • Maximum sum of products occurs at exact alignment

2D Template Matching

Given an image and a small template patch, normalized correlation produces a correlation map where brightness indicates match quality. The global maximum marks the best match location.

Procedure:

  1. Extract or define a template (the pattern to find).

  2. Compute normalized cross-correlation over the entire image.

  3. Locate the peak in the correlation map.

MATLAB/Octave Implementation

1D:

c = normxcorr2(template, signal);
[~, raw_idx] = max(c(:));
idx = raw_idx - length(template) + 1;

2D:

c = normxcorr2(template, image);
[max_val, linear_idx] = max(c(:));
[y_raw, x_raw] = ind2sub(size(c), linear_idx);
y = y_raw - size(template, 1) + 1;
x = x_raw - size(template, 2) + 1;

The offset correction (subtracting template size minus 1) accounts for the fact that correlation output is larger than the input — comparison begins when the template first overlaps the signal.

Limitations of Template Matching

Template matching works well when:

  • The target has a known, fixed appearance (e.g., icons on a screen)

  • Scale and orientation are predictable

  • The template is an exact or near-exact sub-image

Template matching fails or degrades when:

  • Scale variation: target may appear at different sizes (e.g., faces at varying distances)

  • Rotation/pose: target may be oriented differently than the template

  • Deformation: target shape is unpredictable (e.g., cracks, arbitrary lines)

  • Intra-class variation: same category but different appearance (e.g., different people’s faces)

Non-Identical Matching

Even when the template is not an exact sub-image, normalized correlation can find the best approximate match. The correlation coefficient will be less than 1 but may still yield the correct location if the template shares coarse intensity structure with the target.

A blurred or abstracted template (capturing coarse color/intensity layout) can serve as a rough detector when exact appearance is unknown.