v9.1.1
Menu

Nearest Neighbor Distance Ratio (NNDR) Check

easyFREEFeature Detection and MatchingArrays, Sorting, GeometryComputer Vision 1

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

Input
target=[0,0], candidates=[[1,0],[10,0]], threshold=0.5
Output
0
Reasoning
  • Inputs: target is one feature descriptor, here the 2-D point (0,0)(0, 0). candidates holds descriptors from the other image, (1,0)(1, 0) at index 0 and (10,0)(10, 0) at index 1. threshold =0.5= 0.5 is the largest ratio still counted as unambiguous.
  • Distances: the Euclidean distance to (1,0)(1, 0) is (1−0)2+(0−0)2=1\sqrt{(1-0)^2 + (0-0)^2} = 1, and to (10,0)(10, 0) it is 102+02=10\sqrt{10^2 + 0^2} = 10.
  • Nearest two: the closest candidate is index 0 with d1=1d_1 = 1; the runner-up has d2=10d_2 = 10.
  • Ratio test: NNDR=d1/d2=1/10=0.1\text{NNDR} = d_1 / d_2 = 1/10 = 0.1. Since 0.1<0.50.1 < 0.5, 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 1/2=0.51/2 = 0.5, which is not strictly below 0.50.5. Two near-equal candidates make the match ambiguous, so the answer becomes -1.

Learn this in

More from Computer Vision 1

solution.py

Test results

Sign in to run your code against the test cases.