Skip to content
AI360Xpert
Beta

Chamfer Distance for Shapes

A two-way mean of nearest-neighbor gaps that prices how closely two point sets sit on each other.

Chamfer averages each point's nearest gap both ways, so missing and extra parts both cost
Chamfer averages each point's nearest gap both ways, so missing and extra parts both cost

Why Does This Exist?

Point sets have no fixed order, so per-index error fails. Shapes need a permutation-free distance that punishes both missing true points and extra predicted points. Chamfer averages nearest-neighbor distances from truth to prediction plus prediction to truth, giving 3D work one simple shape number.

This page gives the formula, one recomputed line case and misread traps.

Think of It Like This

Two dotted outlines compared dot by dot

Draw a shape in dots twice. For each dot on sheet one, find the closest dot on sheet two and note the gap. Then reverse sheets and repeat. Average all gaps.

Both missing dots and stray dots raise the bill, which one-way checks miss.

How It Actually Works

For sets AA and BB, Chamfer is (1/∣A∣)∑amin⁡b∥a−b∥+(1/∣B∣)∑bmin⁡a∥b−a∥(1/|A|)\sum_{a}\min_b \lVert a-b \rVert + (1/|B|)\sum_{b}\min_a \lVert b-a \rVert, often squared. Lower is better. It is cheap and differentiable but blind to density: clustered points can hide holes.

Worked line example

Take A={0,2}A = \{0, 2\} and B={1,3}B = \{1, 3\}. From AA, gaps are 1,11, 1 averaging 1. From BB, gaps are 1,11, 1 averaging 1. Chamfer is 1+1=21 + 1 = 2. Move BB to {0,2}\{0, 2\} and both averages hit 0, total 0.

Code

Two-way nearest mean on toy 1D sets.

import numpy as np

def chamfer(a: np.ndarray, b: np.ndarray) -> float:    d1 = np.abs(a[:, None] - b[None, :]).min(axis=1).mean()    d2 = np.abs(b[:, None] - a[None, :]).min(axis=1).mean()    return round(float(d1 + d2), 3)

print(chamfer(np.array([0.0, 2.0]), np.array([1.0, 3.0])))# -> 2.0

Gaps 1 and 1 both ways sum to 2.0.

Watch Out For

Trusting Chamfer on uneven density

Dense clusters dominate the mean while sparse holes hide. The symptom is a low score with visible gaps. Fix it by pairing Chamfer with coverage checks or Earth-mover views on critical shapes.

The Quick Version

  • Chamfer is mean nearest gap truth to prediction plus prediction to truth.
  • Toy {0,2}\{0,2\} versus {1,3}\{1,3\} gives 1 plus 1 equals 2.0.
  • Lower is better, zero means matched sets.
  • Use it for simple shape scoring and completion checks.
  • Check density because clusters hide holes.