Non-Maximum Suppression (NMS)
Implement Non-Maximum Suppression (NMS) for 2D point detections.
Given a list of detections, each represented as , and a distance threshold , suppress detections that are within distance of a higher-scoring detection.
Algorithm:
- Sort detections by score in descending order
- Initialize an empty list of kept detections
- For each detection (in score order):
- If it is within Euclidean distance of any already-kept detection, suppress it (skip)
- Otherwise, keep it
The Euclidean distance between two points and is:
Return the surviving detections sorted by score in descending order. Each detection should be returned as with values rounded to 4 decimal places.
Example
detections = [[10, 10, 0.9], [12, 12, 0.8], [50, 50, 0.7], [11, 11, 0.6]] dist_threshold = 5.0
[[10, 10, 0.9], [50, 50, 0.7]]
- Inputs: four point detections, each a location plus a confidence score, and a radius . Detections within of a stronger one are treated as duplicates of the same object.
- Ranked by score the order is . The strongest,
[10, 10, 0.9], has nothing above it, so it survives. [12, 12, 0.8]: distance to is , so it is suppressed.[50, 50, 0.7]: distance to is , so it survives. It is only compared against survivors, never against the suppressed .[11, 11, 0.6]: distance to is , so it is suppressed even though it lies closer than did.- Output:
[[10, 10, 0.9], [50, 50, 0.7]], one peak per cluster, highest score first. Notice that a point at exactly distance 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