Chamfer Distance for Shapes
A two-way mean of nearest-neighbor gaps that prices how closely two point sets sit on each other.
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 and , Chamfer is , often squared. Lower is better. It is cheap and differentiable but blind to density: clustered points can hide holes.
Worked line example
Take and . From , gaps are averaging 1. From , gaps are averaging 1. Chamfer is . Move to 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.0Gaps 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 versus 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.