PIXELBANKv9.1.0
Menu

Non-Maximum Suppression (NMS)

You are given a list of bounding box detections with confidence scores and must apply Non-Maximum Suppression to remove redundant overlapping detections.

Object detectors often produce multiple overlapping boxes for the same object. NMS keeps only the most confident detection and removes boxes that overlap significantly with it.

The algorithm:

  1. Sort all boxes by confidence score (descending)
  2. Select the highest-scoring box, add to keep list
  3. Remove all boxes with IoU > threshold with the selected box
  4. Repeat steps 2-3 until no boxes remain
  5. Return indices of kept boxes

IoU(A,B)=∣A∩B∣∣AβˆͺB∣IoU(A, B) = \frac{|A \cap B|}{|A \cup B|}

Boxes with IoU > threshold are considered to be detecting the same object.

Example:

Input:
boxes = [[0,0,10,10,0.9], [1,1,11,11,0.8], [50,50,60,60,0.7]]
iou_threshold = 0.5
Output:
[0, 2]
Reasoning:
  1. Sort by score: boxes are already sorted (0.9, 0.8, 0.7)

  2. Iteration 1:

    • Select box 0 (score=0.9), add to keep: [0]
    • Check IoU with remaining boxes:
      • IoU(box0, box1) β‰ˆ 0.68 > 0.5 β†’ suppress box 1
      • IoU(box0, box2) = 0 ≀ 0.5 β†’ keep box 2
    • Remaining: [box 2]
  3. Iteration 2:

    • Select box 2 (score=0.7), add to keep: [0, 2]
    • No remaining boxes
  4. Return sorted indices: [0, 2]

Box 1 was suppressed because it overlaps significantly with the higher-scoring box 0.

Constraints:

  • boxes: list of [x1, y1, x2, y2, score] where score is confidence
  • iou_threshold: boxes with IoU > threshold are suppressed (typically 0.5)
  • Return list of indices of kept boxes, sorted in ascending order
πŸ”’

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

0/0
Run code to see test results.
Non-Maximum Suppression (NMS) - Hard | PixelBank