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
- Inputs:
targetis one feature descriptor, here the 2-D point .candidatesholds descriptors from the other image, at index 0 and at index 1.thresholdis the largest ratio still counted as unambiguous. - Distances: the Euclidean distance to is , and to it is .
- Nearest two: the closest candidate is index 0 with ; the runner-up has .
- Ratio test: . Since , the match is accepted and the output is the nearest candidate's index,
0. - Notice: the output is a position in
candidates, not a distance. In the second example the runner-up sits at distance 2, so the ratio is , which is not strictly below . Two near-equal candidates make the match ambiguous, so the answer becomes-1.