Um momento
0xC0Lesson 13 of 16

Remove duplicate detections with non-maximum suppression

Keep one box per object by suppressing weaker boxes that overlap a stronger one.

20 min 6-question quiz 1 code exercise
By the end of this lesson you can
  • Explain why detectors produce duplicate boxes
  • Run non-maximum suppression step by step
  • Choose the overlap threshold

Detectors check many candidate positions, so one object usually sets off a cluster of overlapping boxes, each with a confidence score. Non-maximum suppression (NMS) cleans this up: keep the most confident box, delete the boxes that overlap it too much (they are probably the same object), and repeat with the next most confident box still standing.

Try it

Step through NMS

Two cats sitting close together, five boxes. Step through the algorithm and read why each box is kept or suppressed.

  • How many objects does it find at threshold 0.5?
  • Slide the threshold up to 0.9: what goes wrong?
  • Slide it down to 0.1: what goes wrong now?
1: 0.922: 0.813: 0.674: 0.885: 0.55
  1. Box 1 score 0.92next
  2. Box 4 score 0.88waiting
  3. Box 2 score 0.81waiting
  4. Box 3 score 0.67waiting
  5. Box 5 score 0.55waiting

Detectors often fire several times on the same object. Press “Next step” to process the boxes from highest score to lowest.

The overlap threshold is a trade-off. Too high, and duplicates survive (one cat counted three times). Too low, and genuinely different objects that overlap - two people in a crowd - get merged into one. 0.5 is a common default.

Key takeaways

  • Detectors fire several times per object; NMS keeps one box each.

  • Greedy loop: keep the highest score, drop boxes overlapping it above the threshold, repeat.

  • Too high a threshold keeps duplicates; too low merges neighbors.

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: apply computer vision with Python

Use small pixel arrays to explore vision concepts, run your code against sample images, and connect each result to the larger computer vision idea.

Exercise 1

Implement NMS

+25 XP

The first line is N, then N boxes, one per line, as score x1 y1 x2 y2. The last line is the overlap threshold.

Run non-maximum suppression: go through the boxes from highest to lowest score; keep a box unless its IoU with an already kept box is above the threshold. Print the kept boxes’ numbers (1-based, in input order) in the order they were kept, separated by spaces.

  • Two objects, one duplicate
  • Best box listed last
  • Generous threshold keeps both
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: