← Portfolio

Why a match score hits 100

Tech note · Pattern matching · Related project Fixing pattern-match score misjudgment

What looked wrong

A wafer rotated 90° passed as normal. The match results gave 100 points to both the wafer in normal orientation and the rotated one. The rotated wafer did not have the same shape as the reference pattern, yet its score was the same.

Matching on a normal and a 90-degree-rotated synthetic die pattern. Old score 100 for both; after the fix 97 and 31
Reproduced with a synthetic pattern. The old method gives both wafers 100 points; the fixed method gives 97 and 31. The figure is made by fig_score in tools/make_figures.py

Cause: the score was compared only within that image

Template matching computes how similar the image is to the template at every position and builds a score map. The old code stretched this map to 0 to 100 using the map’s own minimum and maximum. Then the highest position always scores 100, whatever the map looks like.

This score does not say “how similar is this to the reference pattern.” It only says “where in this image is the best match, relative to the rest.” That is fine for finding the position, but wrong when used to decide pass or fail.

# Old: normalize by the map's own min and max. The peak is always 100
m = cv2.matchTemplate(img, tpl, cv2.TM_CCOEFF_NORMED)
score = (m.max() - m.min()) / (m.max() - m.min()) * 100   # = 100

# Fixed: normalize by the range of values the matching mode can produce
# Normalized correlation (TM_CCOEFF_NORMED) is -1 to 1; no similarity or below scores 0
score = max(m.max(), 0) * 100

What I fixed

Takeaway

Normalization changes what a score is compared against. If you do not separate an absolute reference (the theoretical range) from a relative one (rank within the image), the code finds the right position but makes the wrong decision. Before using a score as a pass criterion, I first check: “Is there any case where this score cannot be 100?”

The figure and code reproduce the principle with synthetic data. They are not code or images from the equipment.