v9.1.1
Menu

Non-Maximum Suppression (NMS)

easyPROFeature Detection and MatchingComputer Vision 1

Implement Non-Maximum Suppression (NMS) for 2D point detections.

Given a list of detections, each represented as [x,y,score][x, y, score], and a distance threshold dd, suppress detections that are within distance dd of a higher-scoring detection.

Algorithm:

  1. Sort detections by score in descending order
  2. Initialize an empty list of kept detections
  3. For each detection (in score order):
    • If it is within Euclidean distance dd of any already-kept detection, suppress it (skip)
    • Otherwise, keep it

The Euclidean distance between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is: d=(x1−x2)2+(y1−y2)2d = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}

Return the surviving detections sorted by score in descending order. Each detection should be returned as [x,y,score][x, y, score] with values rounded to 4 decimal places.

Example

Input
detections = [[10, 10, 0.9], [12, 12, 0.8], [50, 50, 0.7], [11, 11, 0.6]]
dist_threshold = 5.0
Output
[[10, 10, 0.9], [50, 50, 0.7]]
Reasoning
  • Inputs: four point detections, each a location (x,y)(x, y) plus a confidence score, and a radius d=5.0d = 5.0. Detections within dd of a stronger one are treated as duplicates of the same object.
  • Ranked by score the order is 0.9,0.8,0.7,0.60.9, 0.8, 0.7, 0.6. The strongest, [10, 10, 0.9], has nothing above it, so it survives.
  • [12, 12, 0.8]: distance to (10,10)(10, 10) is 22+22=8≈2.83≤5\sqrt{2^2 + 2^2} = \sqrt{8} \approx 2.83 \le 5, so it is suppressed.
  • [50, 50, 0.7]: distance to (10,10)(10, 10) is 402+402≈56.57>5\sqrt{40^2 + 40^2} \approx 56.57 > 5, so it survives. It is only compared against survivors, never against the suppressed (12,12)(12, 12).
  • [11, 11, 0.6]: distance to (10,10)(10, 10) is 2≈1.41≤5\sqrt{2} \approx 1.41 \le 5, so it is suppressed even though it lies closer than (12,12)(12, 12) did.
  • Output: [[10, 10, 0.9], [50, 50, 0.7]], one peak per cluster, highest score first. Notice that a point at exactly distance dd also counts as "within" and is suppressed.

Constraints

  • detections: List of [x, y, score]
  • dist_threshold: float (distance threshold for suppression)
  • Return: List of surviving detections sorted by score descending
  • Round values to 4 decimal places
  • Use pure Python + math

Learn this in

More from Computer Vision 1

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.