Object detectors often produce multiple overlapping bounding boxes for the same object. Non-Maximum Suppression (NMS) filters these duplicates by keeping only the highest-scoring box in each cluster of overlapping detections.
Given a list of bounding boxes (each as [x1, y1, x2, y2]), their confidence scores, and an IoU threshold, apply NMS and return the indices of the kept boxes.
To compare two boxes, compute Intersection over Union. The intersection rectangle has corners:
x1′=max(x1A,x1B)y1′=max(y1A,y1B)x2′=min(x2A,x2B)y2′=min(y2A,y2B)The intersection area is max(0, x2' - x1') * max(0, y2' - y1'). If the boxes do not overlap, this is zero.
IoU=Area(A)+Area(B)−IntersectionIntersectionReturn a list of the original indices of the kept boxes, in the order they were selected (highest score first). Return an empty list if the input is empty.
Input:
boxes = [[0, 0, 4, 4], [1, 0, 5, 4]] scores = [0.9, 0.8] iou_threshold = 0.5
Output: [0]
Box 0 (score 0.9) is selected first. Box 1 has IoU of 0.6 with Box 0, which exceeds the threshold, so it is suppressed.
Input:
boxes = [[0, 0, 2, 2], [5, 5, 7, 7], [10, 10, 12, 12]] scores = [0.7, 0.9, 0.8] iou_threshold = 0.5
Output: [1, 2, 0]
No boxes overlap, so all are kept. They are returned in order of decreasing score.
Sort indices by score first, then greedily select and filter.
You need an IoU function as a subroutine. Boxes that are suppressed should not suppress other boxes later.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Object detectors often produce multiple overlapping bounding boxes for the same object. Non-Maximum Suppression (NMS) filters these duplicates by keeping only the highest-scoring box in each cluster of overlapping detections.
Given a list of bounding boxes (each as [x1, y1, x2, y2]), their confidence scores, and an IoU threshold, apply NMS and return the indices of the kept boxes.
To compare two boxes, compute Intersection over Union. The intersection rectangle has corners:
x1′=max(x1A,x1B)y1′=max(y1A,y1B)x2′=min(x2A,x2B)y2′=min(y2A,y2B)The intersection area is max(0, x2' - x1') * max(0, y2' - y1'). If the boxes do not overlap, this is zero.
IoU=Area(A)+Area(B)−IntersectionIntersectionReturn a list of the original indices of the kept boxes, in the order they were selected (highest score first). Return an empty list if the input is empty.
Input:
boxes = [[0, 0, 4, 4], [1, 0, 5, 4]] scores = [0.9, 0.8] iou_threshold = 0.5
Output: [0]
Box 0 (score 0.9) is selected first. Box 1 has IoU of 0.6 with Box 0, which exceeds the threshold, so it is suppressed.
Input:
boxes = [[0, 0, 2, 2], [5, 5, 7, 7], [10, 10, 12, 12]] scores = [0.7, 0.9, 0.8] iou_threshold = 0.5
Output: [1, 2, 0]
No boxes overlap, so all are kept. They are returned in order of decreasing score.
Sort indices by score first, then greedily select and filter.
You need an IoU function as a subroutine. Boxes that are suppressed should not suppress other boxes later.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number