Nearest Neighbor Distance Ratio (NNDR) Check
When matching feature descriptors between images, the Nearest Neighbor Distance Ratio (NNDR) test filters out ambiguous matches. A match is reliable only if:
NNDR = dโ / dโ < threshold
where dโ is the distance to nearest neighbor and dโ is distance to second nearest.
Given a target descriptor and list of candidates, return the index of the nearest neighbor if NNDR < threshold, otherwise return -1.
Constraints:
- 1 โค |descriptor| โค 128
- |candidates| โฅ 2
- 0.0 < threshold < 1.0
Examples:
| Input | Output | |-------|--------| | target=[0,0], candidates=[[1,0],[10,0]], T=0.5 | 0 | | target=[0,0], candidates=[[1,0],[2,0]], T=0.5 | -1 |
Example:
target=[0,0], candidates=[[1,0],[10,0]], threshold=0.5
0
-
Compute distances from target [0,0] to each candidate using Euclidean distance:
- To [1,0]: d1โ=(1โ0)2+(0โ0)2โ=1
- To [10,0]: d2โ=(10โ0)2+(0โ0)2โ=10
-
Sort distances: nearest neighbor distance d1โ=1 (index 0), second nearest d2โ=10 (index 1).
-
Compute NNDR: NNDR=d1โ/d2โ=1/10=0.1.
-
Compare with threshold: 0.1<0.5, so the match is accepted and we return the index of the nearest neighbor, which is 0.
1. Background Knowledge
The Nearest Neighbor Distance Ratio (NNDR) test, introduced by David Lowe in SIFT (2004), is the gold standard for feature matching. It compares the distances to the two nearest neighbors:
NNDR=d2โd1โโ
where d1โ is distance to nearest neighbor and d2โ is distance to second nearest.
Intuition: A distinctive feature should have a much closer match than any alternative. A low ratio indicates high confidence.
Typical thresholds:
- 0.7-0.8 for high precision
- 0.9 for high recall
2. Algorithm Approach
- Compute distances from target to all candidates
- Find two smallest distances (d1โ, d2โ)
- Compute ratio d1โ/d2โ
- Accept match if ratio < threshold
3. Step-by-Step Strategy
- Iterate through candidates computing Euclidean distances
- Sort by distance to find nearest two
- Handle edge case where d2โ=0
- Return matched index or -1
4. Common Pitfalls
- Division by zero when d2โ=0
- Confusing d1โ/d2โ vs d2โ/d1โ
- Using wrong distance metric (should be Euclidean for SIFT)
5. Time & Space Complexity
| Aspect | Complexity |
|---|---|
| Time | O(nยทd) where n=candidates, d=dimensions |
| Space | O(n) for storing distances |