Skip to content
AI360Xpert
Beta

Shape Matching

Compress each outline into seven numbers that survive rotation, scaling, and shifting, then identify parts by a single distance score between signatures.

Seven Hu numbers ignore pose: square and rotated square share phi1 = 1/6 (distance 0.00) while a 2:1 bar gives 5/24 (distance 0.079)
Seven Hu numbers ignore pose: square and rotated square share phi1 = 1/6 (distance 0.00) while a 2:1 bar gives 5/24 (distance 0.079)

Why Does This Exist?

A wrench rotated 4040 degrees on a conveyor is still a wrench, but template matching by pixels fails the moment anything moves. Inspection needs identity divorced from pose: same part at any angle, position, or size must score as same. Hu moments (1962) deliver that by distilling contour measurements into seven invariants, and matchShapes turns two such signatures into one comparable distance.

Pose-proof measurement is the prerequisite, so moments and central moments come first. Fine concavity detail that moments smooth away is covered by convex hulls; texture-based identity instead of outline identity is HOG features.

Think of It Like This

Identifying cutters blindfolded

Hand someone two cookie cutters blindfolded and they feel proportions, not pictures: this one is twice as long as wide with a deep notch, that one is round all over. Size of hand, rotation of grip, and position on the table change nothing about the verdict. Hu moments are that sense of proportion written as seven numbers.

It stops holding for near-twins. Blind touch separates stars from hearts but not two star cutters differing by one bump, the same way Hu signatures blur fine detail. Coarse identity comes free; fine discrimination needs more features.

How It Actually Works

From moments to invariants

Central moments remove translation, normalization by area removes scale, and seven nonlinear combinations (the Hu set) additionally cancel rotation and mirror effects. The pipeline is strictly layered: raw moments from contour properties, then central, then normalized, then the seven invariants. Each layer discards one nuisance variation while keeping shape information, which is why the signature of a rotated wrench matches the upright template near zero distance while a hammer scores far away.

One distance, three flavors

matchShapes compares two signatures with a choice of metric: I1I_1 sums absolute log-differences, I2I_2 uses a chi-square style ratio, I3I_3 a relative form. In practice all three agree on ranking; I1I_1 is the default workhorse. Distances near zero mean same shape up to pose; genuinely different outlines score orders of magnitude higher. Calibrate the same-versus-different boundary on your own parts, since absolute values shift with contour resolution and approximation strength.

Approximation before signature

Hu invariants amplify high-order moments, which amplify boundary noise even faster. Approximate the contour first (approxPolyDP near 2%2\% of perimeter) so both template and candidate carry the same smoothing. Matching a crisp template against a jagged detection punishes the detection for noise rather than shape.

Code

The standard comparison call shape:

import cv2
score_same = cv2.matchShapes(template_cnt, rotated_wrench_cnt, cv2.CONTOURS_MATCH_I1, 0.0)score_other = cv2.matchShapes(template_cnt, hammer_cnt, cv2.CONTOURS_MATCH_I1, 0.0)# same part at any pose scores near zero; a different tool scores far higher

Both contours should be approximated identically first; the score gap between same and different is the decision signal, not any absolute cutoff.

Watch Out For

Expecting near-twins to separate

Symptom: two part revisions differing by one notch score almost identically and pass as each other. Hu moments are deliberately coarse, seven numbers cannot hold fine detail. Fix it by pairing the score with concavity features (hull defects), aspect ratio, or a second classifier for the confused pair.

Matching across different smoothings

Symptom: identical shapes score poorly because the template was traced clean and the detection is jagged, or vice versa. High-order invariants magnify boundary noise. Apply the same approximation epsilon relative to each contour's own perimeter on both sides before computing signatures.

The Quick Version

  • Hu moments compress a contour into seven numbers invariant to translation, scale, and rotation.
  • matchShapes returns one distance: near zero for same shape, far higher for different outlines.
  • Invariants layer strictly: raw, then central, then normalized, then the seven combinations.
  • Approximate both contours identically first, since high-order moments amplify boundary noise.
  • Coarse identity comes free; near-twin discrimination needs hull or texture features alongside.