IoU and NMS Utils¶
supervision.detection.utils.iou_and_nms.OverlapFilter
¶
Bases: Enum
Enum specifying the strategy for filtering overlapping detections.
Attributes:
| Name | Type | Description |
|---|---|---|
NONE |
Do not filter detections based on overlap. |
|
NON_MAX_SUPPRESSION |
Filter detections using non-max suppression. This means, detections that overlap by more than a set threshold will be discarded, except for the one with the highest confidence. |
|
NON_MAX_MERGE |
Merge detections with non-max merging. This means, detections that overlap by more than a set threshold will be merged into a single detection. |
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.OverlapMetric
¶
Bases: Enum
Enum specifying the metric for measuring overlap between detections.
Attributes:
| Name | Type | Description |
|---|---|---|
IOU |
Intersection over Union. A region-overlap metric that compares two shapes (usually bounding boxes or masks) by normalising the shared area with the area of their union. |
|
IOS |
Intersection over Smaller, a region-overlap metric that compares two shapes (usually bounding boxes or masks) by normalising the shared area with the smaller of the two shapes. |
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_iou(box_true: list[float] | npt.NDArray[np.floating], box_detection: list[float] | npt.NDArray[np.floating], overlap_metric: OverlapMetric | str = OverlapMetric.IOU) -> float
¶
Compute overlap metric between two bounding boxes.
Supports standard IOU (intersection-over-union) and IOS
(intersection-over-smaller-area) metrics. Returns the overlap value in range
[0, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[float] | NDArray[floating]
|
Ground truth box in format
|
required |
|
list[float] | NDArray[floating]
|
Detected box in format
|
required |
|
OverlapMetric | str
|
Overlap type.
Use |
IOU
|
Returns:
| Type | Description |
|---|---|
float
|
Overlap value between boxes in |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import supervision as sv
>>> box_true = [100, 100, 200, 200]
>>> box_detection = [150, 150, 250, 250]
>>> sv.box_iou(box_true, box_detection, overlap_metric=sv.OverlapMetric.IOU)
0.142857...
>>> sv.box_iou(box_true, box_detection, overlap_metric=sv.OverlapMetric.IOS)
0.25
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_iou_batch(boxes_true: npt.NDArray[np.number], boxes_detection: npt.NDArray[np.number], overlap_metric: OverlapMetric | str = OverlapMetric.IOU) -> npt.NDArray[np.float32]
¶
Compute pairwise overlap scores between batches of bounding boxes.
Supports standard IOU (intersection-over-union) and IOS
(intersection-over-smaller-area) metrics for all boxes_true and
boxes_detection pairs. Returns a matrix of overlap values in range
[0, 1], matching each box from the first batch to each from the second.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
Array of reference boxes in
shape |
required |
|
NDArray[number]
|
Array of detected boxes in
shape |
required |
|
OverlapMetric | str
|
Overlap type.
Use |
IOU
|
Returns:
| Type | Description |
|---|---|
NDArray[float32]
|
Overlap matrix of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> boxes_true = np.array([
... [100, 100, 200, 200],
... [300, 300, 400, 400]
... ])
>>> boxes_detection = np.array([
... [150, 150, 250, 250],
... [320, 320, 420, 420]
... ])
>>> sv.box_iou_batch(
... boxes_true, boxes_detection, overlap_metric=sv.OverlapMetric.IOU
... )
array([[0.14285..., 0. ],
[0. , 0.47058...]], dtype=float32)
>>> sv.box_iou_batch(
... boxes_true, boxes_detection, overlap_metric=sv.OverlapMetric.IOS
... )
array([[0.25, 0. ],
[0. , 0.64]], dtype=float32)
Source code in src/supervision/detection/utils/iou_and_nms.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
supervision.detection.utils.iou_and_nms.box_iou_batch_with_jaccard(boxes_true: list[list[float]], boxes_detection: list[list[float]], is_crowd: list[bool]) -> npt.NDArray[np.float64]
¶
Calculate the intersection over union (IoU) between detection bounding boxes (dt) and ground-truth bounding boxes (gt). Reference: https://github.com/rafaelpadilla/review_object_detection_metrics
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[list[float]]
|
List of ground-truth bounding boxes in the format [x, y, width, height]. |
required |
|
list[list[float]]
|
List of detection bounding boxes in the format [x, y, width, height]. |
required |
|
list[bool]
|
List indicating if each ground-truth bounding box is a crowd region or not. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Array of IoU values of shape (len(dt), len(gt)). |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> boxes_true = [
... [10, 20, 30, 40], # x, y, w, h
... [15, 25, 35, 45]
... ]
>>> boxes_detection = [
... [12, 22, 28, 38],
... [16, 26, 36, 46]
... ]
>>> is_crowd = [False, False]
>>> ious = sv.box_iou_batch_with_jaccard(
... boxes_true=boxes_true,
... boxes_detection=boxes_detection,
... is_crowd=is_crowd
... )
>>> ious # doctest: +ELLIPSIS
array([[0.886..., 0.496...],
[0.4 ..., 0.862...]])
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.mask_iou_batch(masks_true: npt.NDArray[Any], masks_detection: npt.NDArray[Any], overlap_metric: OverlapMetric = OverlapMetric.IOU, memory_limit: int = 1024 * 5) -> npt.NDArray[np.floating]
¶
Compute Intersection over Union (IoU) of two sets of masks -
masks_true and masks_detection.
Accepts both dense (N, H, W) boolean arrays and
:class:~supervision.detection.compact_mask.CompactMask objects.
When both inputs are :class:~supervision.detection.compact_mask.CompactMask,
the computation uses :func:compact_mask_iou_batch to avoid materialising
full (N, H, W) arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[Any]
|
3D |
required |
|
NDArray[Any]
|
3D |
required |
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of masks (e.g., IoU, IoS). |
IOU
|
|
int
|
Memory limit in MB, default is 1024 * 5 MB (5GB).
Controls chunking of |
1024 * 5
|
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Pairwise IoU of masks from |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/supervision/detection/utils/iou_and_nms.py
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 | |
supervision.detection.utils.iou_and_nms.oriented_box_iou_batch(boxes_true: npt.NDArray[np.number], boxes_detection: npt.NDArray[np.number], overlap_metric: OverlapMetric = OverlapMetric.IOU) -> npt.NDArray[np.floating]
¶
Compute pairwise overlap scores between two sets of oriented bounding boxes
using the configured overlap_metric.
Overlap areas are computed exactly via convex-polygon intersection, gated by a cheap axis-aligned envelope pre-filter — no rasterization is involved, so the result is exact (free of pixel-quantization error) and independent of the coordinate magnitudes.
boxes_true and boxes_detection are expected to be in
((x1, y1), (x2, y2), (x3, y3), (x4, y4)) format.
Note
Inputs must be convex quads with finite coordinates. Self-intersecting
or non-convex polygons produce undefined results via
cv2.intersectConvexConvex. NaN or Inf coordinates propagate silently
as 0.0 — validate inputs before calling if needed.
When boxes_true is boxes_detection (the same Python object, not just
equal values), the function computes only the upper triangle of the
matrix and mirrors it. This optimization is used automatically by the
NMS/NMM callers that pass the same array twice. A defensive .copy()
at the call site would disable the optimization silently — see the
NMS caller comment for context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
A |
required |
|
NDArray[number]
|
A |
required |
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of oriented boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Overlap matrix of shape |
NDArray[floating]
|
score between |
NDArray[floating]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
ValueError
|
If |
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> a = np.array([[[0, 0], [2, 0], [2, 2], [0, 2]]], dtype=np.float32)
>>> b = np.array([[[1, 0], [3, 0], [3, 2], [1, 2]]], dtype=np.float32)
>>> sv.oriented_box_iou_batch(a, b)
array([[0.333...]])
Source code in src/supervision/detection/utils/iou_and_nms.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 | |
supervision.detection.utils.iou_and_nms.box_non_max_suppression(predictions: npt.NDArray[np.floating], iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> npt.NDArray[np.bool_]
¶
Perform Non-Maximum Suppression (NMS) on object detection predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
An array of object detection predictions in
the format of |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
A boolean array indicating which predictions to keep after non-maximum suppression. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.mask_non_max_suppression(predictions: npt.NDArray[np.floating], masks: npt.NDArray[Any], iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU, mask_dimension: int = 640) -> npt.NDArray[np.bool_]
¶
Perform Non-Maximum Suppression (NMS) on segmentation predictions.
IoU is computed exactly on the full-resolution masks for both dense and
:class:~supervision.detection.compact_mask.CompactMask inputs. The
mask_dimension parameter is kept for backward compatibility but is no
longer used — dense masks are not resized before IoU computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
A 2D array of object detection predictions in
the format of |
required |
|
NDArray[Any]
|
A 3D array of binary masks corresponding to the predictions.
Shape: |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of masks (e.g., IoU, IoS). |
IOU
|
|
int
|
Deprecated, no longer used. Kept for backward compatibility. |
640
|
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
A boolean array indicating which predictions to keep after non-maximum suppression. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.box_non_max_merge(predictions: npt.NDArray[np.float64], iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> list[list[int]]
¶
Apply greedy version of non-maximum merging per category to avoid detecting too many overlapping bounding boxes for a given object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[float64]
|
An array of shape |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. Defaults to 0.5. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
list[list[int]]: Groups of prediction indices be merged. Each group may have 1 or more elements. |
Source code in src/supervision/detection/utils/iou_and_nms.py
supervision.detection.utils.iou_and_nms.mask_non_max_merge(predictions: npt.NDArray[np.floating], masks: npt.NDArray[Any], iou_threshold: float = 0.5, mask_dimension: int = 640, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> list[list[int]]
¶
Perform Non-Maximum Merging (NMM) on segmentation predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
A 2D array of object detection predictions in
the format of |
required |
|
NDArray[Any]
|
A 3D array of binary masks corresponding to the predictions.
Shape: |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
|
int
|
The dimension to which the masks should be resized before computing IOU values. Defaults to 640. |
640
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of masks (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
A list of groups of prediction indices. Each inner list contains
the indices of predictions whose masks overlap above |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in src/supervision/detection/utils/iou_and_nms.py
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 | |
supervision.detection.utils.iou_and_nms.oriented_box_non_max_suppression(predictions: npt.NDArray[np.floating], oriented_boxes: npt.NDArray[np.floating], iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> npt.NDArray[np.bool_]
¶
Perform Non-Maximum Suppression on oriented bounding box predictions.
Overlap is computed via :func:oriented_box_iou_batch on the four
corners of each box, so detections whose axis-aligned bounding boxes
overlap heavily but whose oriented bodies do not are kept — unlike
:func:box_non_max_suppression, which would suppress them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
An array of object detection predictions in the
format |
required |
|
NDArray[floating]
|
Array of shape |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum suppression. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of oriented boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
NDArray[bool_]
|
A boolean array of shape |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> oriented_boxes = np.array([
... [[10, 10], [50, 10], [50, 30], [10, 30]],
... [[11, 11], [51, 11], [51, 31], [11, 31]],
... ], dtype=np.float32)
>>> predictions = np.array([
... [10, 10, 50, 30, 0.9, 0],
... [11, 11, 51, 31, 0.8, 0],
... ], dtype=np.float32)
>>> keep = sv.oriented_box_non_max_suppression(
... predictions=predictions,
... oriented_boxes=oriented_boxes,
... iou_threshold=0.5,
... )
>>> keep
array([ True, False])
Source code in src/supervision/detection/utils/iou_and_nms.py
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 | |
supervision.detection.utils.iou_and_nms.oriented_box_non_max_merge(predictions: npt.NDArray[np.floating], oriented_boxes: npt.NDArray[np.floating], iou_threshold: float = 0.5, overlap_metric: OverlapMetric = OverlapMetric.IOU) -> list[list[int]]
¶
Perform Non-Maximum Merging on oriented bounding box predictions, grouped per category.
Mirrors :func:box_non_max_merge but uses oriented-box IoU, so groups
of rotated detections sharing the same body — rather than the same
axis-aligned bounding box — are merged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
An array of shape |
required |
|
NDArray[floating]
|
Array of shape |
required |
|
float
|
The intersection-over-union threshold to use for non-maximum merging. |
0.5
|
|
OverlapMetric
|
Metric used to compute the degree of overlap between pairs of oriented boxes (e.g., IoU, IoS). |
IOU
|
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
Groups of prediction indices to be merged. Each group may have 1 or more elements. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> oriented_boxes = np.array([
... [[10, 10], [50, 10], [50, 30], [10, 30]],
... [[11, 11], [51, 11], [51, 31], [11, 31]],
... ], dtype=np.float32)
>>> predictions = np.array([
... [10, 10, 50, 30, 0.9, 0],
... [11, 11, 51, 31, 0.8, 0],
... ], dtype=np.float32)
>>> groups = sv.oriented_box_non_max_merge(
... predictions=predictions,
... oriented_boxes=oriented_boxes,
... iou_threshold=0.5,
... )
>>> len(groups)
1
Source code in src/supervision/detection/utils/iou_and_nms.py
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 | |