Skip to content

CompactMask

supervision.detection.compact_mask.CompactMask

Memory-efficient crop-RLE mask storage for instance segmentation.

Instead of storing N full (H, W) boolean arrays, :class:CompactMask encodes each mask as a run-length sequence of its bounding-box crop. This reduces memory from O(N·H·W) to roughly O(N·bbox_area), which is orders of magnitude smaller for sparse masks on high-resolution images.

The class exposes a duck-typed interface compatible with np.ndarray masks used elsewhere in supervision:

  • mask[int] → dense (H, W) bool array (annotators, converters).
  • mask[slice | list | ndarray] → new :class:CompactMask (filtering).
  • np.asarray(mask) → dense (N, H, W) bool array (numpy interop).
  • mask.shape, mask.dtype, mask.area — match the dense API.

:class:CompactMask is not a drop-in np.ndarray replacement. When you need to call arbitrary ndarray methods (astype, reshape, ravel, any, all, …) call :meth:to_dense first: cm.to_dense().astype(np.uint8). :meth:to_dense is the single explicit materialisation boundary.

.. note:: RLE encoding — COCO / pycocotools pixel-scan order

:class:`CompactMask` uses **column-major (Fortran-order, F-order)**
run-lengths scoped to each mask's bounding-box crop, matching the
pixel-scan order used by the COCO API (pycocotools).  The crop scope
still differs from the full-image scope used by pycocotools, so a
:class:`CompactMask` RLE cannot be passed directly to
``maskUtils.iou()`` or ``maskUtils.decode()`` without re-scoping to
the full canvas.  Use :meth:`to_dense` to obtain a standard boolean
array for pycocotools interop.

This scan order is part of CompactMask's internal RLE representation.
Switching from row-major (C-order) to column-major (F-order) is a
backward-incompatible format change for any persisted or serialized
:class:`CompactMask` state, including pickled objects and any
external storage of ``._rles``.  Older stored RLE arrays will decode
incorrectly under the new convention.

Migration note: load or decode legacy masks with the older version,
materialize them to dense boolean arrays, and then re-encode them
with the current version (for example via :meth:`to_dense` followed
by :meth:`from_dense`) before persisting them again.

Parameters:

Name Type Description Default

rles

list[NDArray[int32]]

List of N int32 run-length arrays.

required

crop_shapes

NDArray[int32]

Array of shape (N, 2)(crop_h, crop_w) per mask.

required

offsets

NDArray[int32]

Array of shape (N, 2)(x1, y1) bounding-box origins.

required

image_shape

tuple[int, int]

(H, W) of the full image.

required

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((2, 100, 100), dtype=bool)
>>> masks[0, 10:20, 10:20] = True
>>> masks[1, 50:70, 50:80] = True
>>> xyxy = np.array([[10, 10, 19, 19], [50, 50, 79, 69]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
>>> len(cm)
2
>>> cm.shape
(2, 100, 100)
Source code in src/supervision/detection/compact_mask.py
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 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
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 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
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
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
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
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
class CompactMask:
    """Memory-efficient crop-RLE mask storage for instance segmentation.

    Instead of storing N full ``(H, W)`` boolean arrays, :class:`CompactMask`
    encodes each mask as a run-length sequence of its bounding-box crop.  This
    reduces memory from O(N·H·W) to roughly O(N·bbox_area), which is orders of
    magnitude smaller for sparse masks on high-resolution images.

    The class exposes a duck-typed interface compatible with ``np.ndarray``
    masks used elsewhere in ``supervision``:

    * ``mask[int]`` → dense ``(H, W)`` bool array (annotators, converters).
    * ``mask[slice | list | ndarray]`` → new :class:`CompactMask` (filtering).
    * ``np.asarray(mask)`` → dense ``(N, H, W)`` bool array (numpy interop).
    * ``mask.shape``, ``mask.dtype``, ``mask.area`` — match the dense API.

    :class:`CompactMask` is **not** a drop-in ``np.ndarray`` replacement.
    When you need to call arbitrary ndarray methods (``astype``, ``reshape``,
    ``ravel``, ``any``, ``all``, …) call :meth:`to_dense` first:
    ``cm.to_dense().astype(np.uint8)``.  :meth:`to_dense` is the single
    explicit materialisation boundary.

    .. note:: **RLE encoding — COCO / pycocotools pixel-scan order**

        :class:`CompactMask` uses **column-major (Fortran-order, F-order)**
        run-lengths scoped to each mask's bounding-box crop, matching the
        pixel-scan order used by the COCO API (pycocotools).  The crop scope
        still differs from the full-image scope used by pycocotools, so a
        :class:`CompactMask` RLE cannot be passed directly to
        ``maskUtils.iou()`` or ``maskUtils.decode()`` without re-scoping to
        the full canvas.  Use :meth:`to_dense` to obtain a standard boolean
        array for pycocotools interop.

        This scan order is part of CompactMask's internal RLE representation.
        Switching from row-major (C-order) to column-major (F-order) is a
        backward-incompatible format change for any persisted or serialized
        :class:`CompactMask` state, including pickled objects and any
        external storage of ``._rles``.  Older stored RLE arrays will decode
        incorrectly under the new convention.

        Migration note: load or decode legacy masks with the older version,
        materialize them to dense boolean arrays, and then re-encode them
        with the current version (for example via :meth:`to_dense` followed
        by :meth:`from_dense`) before persisting them again.

    Args:
        rles: List of N int32 run-length arrays.
        crop_shapes: Array of shape ``(N, 2)`` — ``(crop_h, crop_w)`` per mask.
        offsets: Array of shape ``(N, 2)`` — ``(x1, y1)`` bounding-box origins.
        image_shape: ``(H, W)`` of the full image.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((2, 100, 100), dtype=bool)
        >>> masks[0, 10:20, 10:20] = True
        >>> masks[1, 50:70, 50:80] = True
        >>> xyxy = np.array([[10, 10, 19, 19], [50, 50, 79, 69]], dtype=np.float32)
        >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
        >>> len(cm)
        2
        >>> cm.shape
        (2, 100, 100)

        ```
    """

    __slots__ = ("_crop_shapes", "_image_shape", "_offsets", "_rles")

    def __init__(
        self,
        rles: list[npt.NDArray[np.int32]],
        crop_shapes: npt.NDArray[np.int32],
        offsets: npt.NDArray[np.int32],
        image_shape: tuple[int, int],
    ) -> None:
        self._rles: list[npt.NDArray[np.int32]] = rles
        self._crop_shapes: npt.NDArray[np.int32] = crop_shapes  # (N,2): (h,w)
        self._offsets: npt.NDArray[np.int32] = offsets  # (N,2): (x1,y1)
        self._image_shape: tuple[int, int] = image_shape  # (H, W)

    # ------------------------------------------------------------------
    # Construction
    # ------------------------------------------------------------------

    @classmethod
    def from_dense(
        cls,
        masks: npt.NDArray[np.bool_],
        xyxy: npt.NDArray[Any],
        image_shape: tuple[int, int],
    ) -> CompactMask:
        """Create a :class:`CompactMask` from a dense ``(N, H, W)`` bool array.

        Bounding boxes are clipped to image bounds and interpreted in the
        supervision ``xyxy`` convention (inclusive max coordinates). A
        box with invalid ordering (``x2 < x1`` or ``y2 < y1``) is replaced by
        a ``1x1`` all-False crop to avoid degenerate RLE.

        Args:
            masks: Dense boolean mask array of shape ``(N, H, W)``.
            xyxy: Bounding boxes of shape ``(N, 4)`` in ``[x1, y1, x2, y2]``
                format.
            image_shape: ``(H, W)`` of the full image.

        Returns:
            A new :class:`CompactMask` instance.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 100, 100), dtype=bool)
            >>> masks[0, 10:20, 10:20] = True
            >>> xyxy = np.array([[10, 10, 19, 19]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
            >>> cm.shape
            (1, 100, 100)

            ```
        """
        img_h, img_w = image_shape
        num_masks = len(masks)

        if num_masks == 0:
            return cls(
                [],
                np.empty((0, 2), dtype=np.int32),
                np.empty((0, 2), dtype=np.int32),
                image_shape,
            )

        rles: list[npt.NDArray[np.int32]] = []
        crop_shapes_list: list[tuple[int, int]] = []
        offsets_list: list[tuple[int, int]] = []

        for mask_idx in range(num_masks):
            x1, y1, x2, y2 = xyxy[mask_idx]
            x1c = int(max(0, min(int(x1), img_w - 1)))
            y1c = int(max(0, min(int(y1), img_h - 1)))
            x2c = int(max(0, min(int(x2), img_w - 1)))
            y2c = int(max(0, min(int(y2), img_h - 1)))
            crop: npt.NDArray[np.bool_]

            # supervision xyxy uses inclusive max coords, so slicing must add +1.
            if x2c < x1c or y2c < y1c:
                crop = np.zeros((1, 1), dtype=bool)
                x2c, y2c = x1c, y1c
            else:
                crop = masks[mask_idx, y1c : y2c + 1, x1c : x2c + 1]

            crop_h = y2c - y1c + 1
            crop_w = x2c - x1c + 1
            rles.append(_mask_to_rle_counts(crop))
            crop_shapes_list.append((crop_h, crop_w))
            offsets_list.append((x1c, y1c))

        crop_shapes = np.array(crop_shapes_list, dtype=np.int32)
        offsets = np.array(offsets_list, dtype=np.int32)
        return cls(rles, crop_shapes, offsets, image_shape)

    # ------------------------------------------------------------------
    # Materialisation
    # ------------------------------------------------------------------

    def to_dense(self) -> npt.NDArray[np.bool_]:
        """Materialise all masks as a dense ``(N, H, W)`` boolean array.

        Returns:
            Boolean array of shape ``(N, H, W)``.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 50, 50), dtype=bool)
            >>> masks[0, 10:20, 10:30] = True
            >>> xyxy = np.array([[10, 10, 29, 19]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(50, 50))
            >>> cm.to_dense().shape
            (1, 50, 50)

            ```
        """
        num_masks = len(self._rles)
        img_h, img_w = self._image_shape
        result: npt.NDArray[np.bool_] = np.zeros((num_masks, img_h, img_w), dtype=bool)
        for mask_idx in range(num_masks):
            crop_h, crop_w = (
                int(self._crop_shapes[mask_idx, 0]),
                int(self._crop_shapes[mask_idx, 1]),
            )
            x1, y1 = int(self._offsets[mask_idx, 0]), int(self._offsets[mask_idx, 1])
            crop = _rle_counts_to_mask(self._rles[mask_idx], crop_h, crop_w)
            result[mask_idx, y1 : y1 + crop_h, x1 : x1 + crop_w] = crop
        return result

    def crop(self, index: int) -> npt.NDArray[np.bool_]:
        """Decode a single mask crop without allocating the full image array.

        This is an O(crop_area) operation — ideal for annotators that only
        need the cropped region.

        Args:
            index: Index of the mask to decode.

        Returns:
            Boolean array of shape ``(crop_h, crop_w)``.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 100, 100), dtype=bool)
            >>> masks[0, 20:30, 10:40] = True
            >>> xyxy = np.array([[10, 20, 39, 29]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
            >>> cm.crop(0).shape
            (10, 30)

            ```
        """
        crop_h = int(self._crop_shapes[index, 0])
        crop_w = int(self._crop_shapes[index, 1])
        return _rle_counts_to_mask(self._rles[index], crop_h, crop_w)

    # ------------------------------------------------------------------
    # Sequence / array protocol
    # ------------------------------------------------------------------

    def __len__(self) -> int:
        """Return the number of masks.

        Returns:
            Number of masks N.

        Examples:
            ```pycon
            >>> from supervision.detection.compact_mask import CompactMask
            >>> import numpy as np
            >>> cm = CompactMask(
            ...     [], np.empty((0, 2), dtype=np.int32),
            ...     np.empty((0, 2), dtype=np.int32), (100, 100))
            >>> len(cm)
            0

            ```
        """
        return len(self._rles)

    def __iter__(self) -> Iterator[npt.NDArray[np.bool_]]:
        """Iterate over masks as dense ``(H, W)`` boolean arrays."""
        for mask_idx in range(len(self)):
            yield self[mask_idx]

    @property
    def shape(self) -> tuple[int, int, int]:
        """Return ``(N, H, W)`` matching the dense mask convention.

        Returns:
            Tuple ``(N, H, W)``.

        Examples:
            ```pycon
            >>> from supervision.detection.compact_mask import CompactMask
            >>> import numpy as np
            >>> cm = CompactMask(
            ...     [], np.empty((0, 2), dtype=np.int32),
            ...     np.empty((0, 2), dtype=np.int32), (480, 640))
            >>> cm.shape
            (0, 480, 640)

            ```
        """
        img_h, img_w = self._image_shape
        return (len(self), img_h, img_w)

    @property
    def offsets(self) -> npt.NDArray[np.int32]:
        """Return per-mask crop origins as ``(x1, y1)`` integer offsets.

        Returns:
            Array of shape ``(N, 2)`` with ``int32`` offsets.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 10, 10), dtype=bool)
            >>> masks[0, 2:4, 3:5] = True
            >>> xyxy = np.array([[3, 2, 4, 3]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
            >>> cm.offsets.tolist()
            [[3, 2]]

            ```
        """
        return self._offsets

    @property
    def bbox_xyxy(self) -> npt.NDArray[np.int32]:
        """Return per-mask inclusive bounding boxes in ``xyxy`` format.

        Boxes are derived from crop metadata:
        ``x2 = x1 + crop_w - 1``, ``y2 = y1 + crop_h - 1``.

        Returns:
            Array of shape ``(N, 4)`` with ``int32`` boxes
            ``[x1, y1, x2, y2]``.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 10, 10), dtype=bool)
            >>> masks[0, 2:5, 3:7] = True
            >>> xyxy = np.array([[3, 2, 6, 4]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
            >>> cm.bbox_xyxy.tolist()
            [[3, 2, 6, 4]]

            ```
        """
        if len(self) == 0:
            return np.empty((0, 4), dtype=np.int32)

        x1: npt.NDArray[np.int32] = self._offsets[:, 0]
        y1: npt.NDArray[np.int32] = self._offsets[:, 1]
        x2: npt.NDArray[np.int32] = x1 + self._crop_shapes[:, 1] - 1
        y2: npt.NDArray[np.int32] = y1 + self._crop_shapes[:, 0] - 1
        return np.column_stack((x1, y1, x2, y2)).astype(np.int32, copy=False)

    @property
    def dtype(self) -> np.dtype[Any]:
        """Return ``np.dtype(bool)`` — always.

        Returns:
            ``np.dtype(bool)``.

        Examples:
            ```pycon
            >>> from supervision.detection.compact_mask import CompactMask
            >>> import numpy as np
            >>> cm = CompactMask(
            ...     [], np.empty((0, 2), dtype=np.int32),
            ...     np.empty((0, 2), dtype=np.int32), (100, 100))
            >>> cm.dtype
            dtype('bool')

            ```
        """
        return np.dtype(bool)

    @property
    def area(self) -> npt.NDArray[np.int64]:
        """Compute the area (``True`` pixel count) of each mask.

        Returns:
            int64 array of shape ``(N,)`` with per-mask pixel counts.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((2, 100, 100), dtype=bool)
            >>> masks[0, 0:10, 0:10] = True  # 100 pixels
            >>> masks[1, 0:5, 0:5] = True    # 25 pixels
            >>> xyxy = np.array([[0, 0, 9, 9], [0, 0, 4, 4]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
            >>> cm.area.tolist()
            [100, 25]

            ```
        """
        return np.array([_rle_area(rle) for rle in self._rles], dtype=np.int64)

    def sum(self, axis: int | tuple[int, ...] | None = None) -> npt.NDArray[Any] | int:
        """NumPy-compatible sum with a fast path for per-mask area.

        When ``axis=(1, 2)``, returns the per-mask True-pixel count via
        :attr:`area` without materialising the full dense array.

        Args:
            axis: Axis or axes to sum over.

        Returns:
            Sum result matching NumPy semantics.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 10, 10), dtype=bool)
            >>> masks[0, 0:3, 0:3] = True
            >>> xyxy = np.array([[0, 0, 2, 2]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
            >>> cm.sum(axis=(1, 2)).tolist()
            [9]

            ```
        """
        if axis == (1, 2):
            return self.area
        return self.to_dense().sum(axis=axis)

    def __getitem__(
        self,
        index: int | slice | list[Any] | npt.NDArray[Any],
    ) -> npt.NDArray[np.bool_] | CompactMask:
        """Index into the mask collection.

        * ``int`` → dense ``(H, W)`` bool array (for annotators, iterators).
        * ``slice | list | ndarray`` → new :class:`CompactMask` (for filtering).

        Args:
            index: An integer returns a dense ``(H, W)`` mask.  Any other
                supported index type returns a new :class:`CompactMask`.

        Returns:
            Dense ``(H, W)`` ``np.ndarray`` for integer index, or a new
            :class:`CompactMask` for all other index types.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((3, 20, 20), dtype=bool)
            >>> xyxy = np.array(
            ...     [[0,0,5,5],[5,5,10,10],[10,10,15,15]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(20, 20))
            >>> cm[0].shape        # int → dense (H, W)
            (20, 20)
            >>> len(cm[[0, 2]])    # list → CompactMask
            2

            ```
        """
        if isinstance(index, (int, np.integer)):
            idx = int(index)
            img_h, img_w = self._image_shape
            result: npt.NDArray[np.bool_] = np.zeros((img_h, img_w), dtype=bool)
            crop_h = int(self._crop_shapes[idx, 0])
            crop_w = int(self._crop_shapes[idx, 1])
            x1 = int(self._offsets[idx, 0])
            y1 = int(self._offsets[idx, 1])
            crop = _rle_counts_to_mask(self._rles[idx], crop_h, crop_w)
            result[y1 : y1 + crop_h, x1 : x1 + crop_w] = crop
            return result

        # Slice: use direct Python list slice and numpy view — O(k), no arange.
        if isinstance(index, slice):
            return CompactMask(
                self._rles[index],
                self._crop_shapes[index],
                self._offsets[index],
                self._image_shape,
            )

        # Boolean selectors and fancy index → convert to integer positions first.
        if isinstance(index, np.ndarray) and index.dtype == bool:
            idx_arr = np.where(index)[0]
        elif isinstance(index, list) and all(
            isinstance(item, (bool, np.bool_)) for item in index
        ):
            idx_arr = np.flatnonzero(np.asarray(index, dtype=bool))
        else:
            idx_arr = np.asarray(list(index), dtype=np.intp)

        new_rles = [self._rles[int(mask_idx)] for mask_idx in idx_arr]
        new_crop_shapes: npt.NDArray[np.int32] = self._crop_shapes[idx_arr]
        new_offsets: npt.NDArray[np.int32] = self._offsets[idx_arr]
        return CompactMask(new_rles, new_crop_shapes, new_offsets, self._image_shape)

    def __array__(self, dtype: np.dtype[Any] | None = None) -> npt.NDArray[Any]:
        """NumPy interop: materialise as a dense ``(N, H, W)`` array.

        Called by ``np.asarray(compact_mask)`` and similar NumPy functions.

        Args:
            dtype: Optional dtype to cast the result to.

        Returns:
            Dense boolean array of shape ``(N, H, W)``.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 10, 10), dtype=bool)
            >>> xyxy = np.array([[0, 0, 5, 5]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
            >>> np.asarray(cm).shape
            (1, 10, 10)

            ```
        """
        result = self.to_dense()
        if dtype is not None:
            return result.astype(dtype)
        return result

    def __eq__(self, other: object) -> bool:
        """Element-wise equality with another :class:`CompactMask` or ndarray.

        Args:
            other: Another :class:`CompactMask` or ``np.ndarray``.

        Returns:
            ``True`` if all masks are pixel-identical.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 10, 10), dtype=bool)
            >>> xyxy = np.array([[0, 0, 5, 5]], dtype=np.float32)
            >>> cm1 = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
            >>> cm2 = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
            >>> cm1 == cm2
            True

            ```
        """
        if isinstance(other, CompactMask):
            return bool(np.array_equal(self.to_dense(), other.to_dense()))
        if isinstance(other, np.ndarray):
            return bool(np.array_equal(self.to_dense(), other))
        return NotImplemented

    # ------------------------------------------------------------------
    # Collection utilities
    # ------------------------------------------------------------------

    @staticmethod
    def merge(masks_list: list[CompactMask]) -> CompactMask:
        """Concatenate multiple :class:`CompactMask` objects into one.

        All inputs must have the same ``image_shape``.

        Args:
            masks_list: Non-empty list of :class:`CompactMask` objects.

        Returns:
            A new :class:`CompactMask` containing every mask from the inputs,
            in order.

        Raises:
            ValueError: If ``masks_list`` is empty or image shapes differ.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks1 = np.zeros((2, 50, 50), dtype=bool)
            >>> masks2 = np.zeros((3, 50, 50), dtype=bool)
            >>> xyxy1 = np.array([[0,0,10,10],[10,10,20,20]], dtype=np.float32)
            >>> xyxy2 = np.array(
            ...     [[0,0,5,5],[5,5,10,10],[10,10,15,15]], dtype=np.float32)
            >>> cm1 = CompactMask.from_dense(masks1, xyxy1, image_shape=(50, 50))
            >>> cm2 = CompactMask.from_dense(masks2, xyxy2, image_shape=(50, 50))
            >>> len(CompactMask.merge([cm1, cm2]))
            5

            ```
        """
        if not masks_list:
            raise ValueError("Cannot merge an empty list of CompactMask objects.")

        image_shape = masks_list[0]._image_shape
        for cm in masks_list[1:]:
            if cm._image_shape != image_shape:
                raise ValueError(
                    f"Cannot merge CompactMask objects with different image shapes: "
                    f"{image_shape} vs {cm._image_shape}"
                )

        # list.extend is a C-level call and avoids the per-element Python
        # bytecode overhead of a flat list comprehension.  This matters under
        # GIL contention when multiple threads call merge concurrently.
        new_rles: list[npt.NDArray[np.int32]] = []
        for cm in masks_list:
            new_rles.extend(cm._rles)

        # np.concatenate handles (0, 2) arrays correctly.
        # No .astype() needed — _crop_shapes and _offsets are already int32.
        new_crop_shapes: npt.NDArray[np.int32] = np.concatenate(
            [cm._crop_shapes for cm in masks_list], axis=0
        )
        new_offsets: npt.NDArray[np.int32] = np.concatenate(
            [cm._offsets for cm in masks_list], axis=0
        )

        return CompactMask(new_rles, new_crop_shapes, new_offsets, image_shape)

    def repack(self) -> CompactMask:
        """Re-encode all masks using tight bounding boxes.

        When the original ``xyxy`` boxes are padded or loose — common with
        object-detector outputs and full-image boxes used in tests — each RLE
        crop encodes more background (``False``) pixels than necessary.  This
        method decodes every crop, trims it to the minimal rectangle that
        contains all ``True`` pixels, and re-encodes.  All-``False`` masks are
        normalised to a ``1x1`` all-``False`` crop.

        The call is O(sum of crop areas) — suitable as a one-time cleanup
        after accumulating many merges (e.g. after
        :class:`~supervision.detection.tools.inference_slicer.InferenceSlicer`
        tiles are merged).

        Returns:
            A new :class:`CompactMask` with minimal-area crops and updated
            offsets.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 10, 10), dtype=bool)
            >>> masks[0, 3:7, 3:7] = True
            >>> # Deliberately loose bbox: covers the full image.
            >>> xyxy = np.array([[0, 0, 9, 9]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
            >>> repacked = cm.repack()
            >>> repacked.offsets.tolist()  # tight origin: x1=3, y1=3
            [[3, 3]]

            ```
        """
        num_masks = len(self._rles)
        if num_masks == 0:
            return CompactMask(
                [],
                np.empty((0, 2), dtype=np.int32),
                np.empty((0, 2), dtype=np.int32),
                self._image_shape,
            )

        new_rles: list[npt.NDArray[np.int32]] = []
        new_crop_shapes_list: list[tuple[int, int]] = []
        new_offsets_list: list[tuple[int, int]] = []

        for mask_idx in range(num_masks):
            crop = self.crop(mask_idx)
            x1_off = int(self._offsets[mask_idx, 0])
            y1_off = int(self._offsets[mask_idx, 1])

            rows_any = np.any(crop, axis=1)
            cols_any = np.any(crop, axis=0)

            if not rows_any.any():
                # All-False: normalise to 1x1 to avoid zero-sized arrays.
                new_rles.append(_mask_to_rle_counts(np.zeros((1, 1), dtype=bool)))
                new_crop_shapes_list.append((1, 1))
                new_offsets_list.append((x1_off, y1_off))
                continue

            y_indices = np.where(rows_any)[0]
            x_indices = np.where(cols_any)[0]
            y_min, y_max = int(y_indices[0]), int(y_indices[-1])
            x_min, x_max = int(x_indices[0]), int(x_indices[-1])

            tight = crop[y_min : y_max + 1, x_min : x_max + 1]
            new_rles.append(_mask_to_rle_counts(tight))
            new_crop_shapes_list.append((y_max - y_min + 1, x_max - x_min + 1))
            new_offsets_list.append((x1_off + x_min, y1_off + y_min))

        return CompactMask(
            new_rles,
            np.array(new_crop_shapes_list, dtype=np.int32),
            np.array(new_offsets_list, dtype=np.int32),
            self._image_shape,
        )

    # ------------------------------------------------------------------
    # Slicer support
    # ------------------------------------------------------------------

    def with_offset(
        self,
        dx: int,
        dy: int,
        new_image_shape: tuple[int, int],
    ) -> CompactMask:
        """Return a new :class:`CompactMask` with adjusted offsets and image shape.

        Used by :class:`~supervision.detection.tools.inference_slicer.InferenceSlicer`
        to relocate tile-local masks into full-image coordinates without
        materialising the dense ``(N, H, W)`` array.

        Args:
            dx: Pixels to add to every mask's ``x1`` offset.
            dy: Pixels to add to every mask's ``y1`` offset.
            new_image_shape: ``(H, W)`` of the full (destination) image.

        Returns:
            New :class:`CompactMask` with updated offsets and image shape.
            Crops are clipped to stay inside ``new_image_shape``; masks fully
            outside are represented as ``1x1`` all-False crops.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 20, 20), dtype=bool)
            >>> xyxy = np.array([[5, 5, 15, 15]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(20, 20))
            >>> cm2 = cm.with_offset(100, 200, new_image_shape=(400, 400))
            >>> cm2.offsets[0].tolist()
            [105, 205]

            ```
        """
        new_h, new_w = new_image_shape
        if new_h <= 0 or new_w <= 0:
            raise ValueError("new_image_shape must contain positive dimensions")

        num_masks = len(self)
        if num_masks == 0:
            return CompactMask(
                [],
                np.empty((0, 2), dtype=np.int32),
                np.empty((0, 2), dtype=np.int32),
                new_image_shape,
            )

        # Vectorised bounds check: compute every new [x1,y1,x2,y2] at once.
        # For the common case (InferenceSlicer tiles that fit fully inside the
        # new canvas) this catches the "no clipping needed" path in O(N) numpy
        # without touching any RLE data.
        new_offsets: npt.NDArray[np.int32] = self._offsets + np.array(
            [dx, dy], dtype=np.int32
        )
        x1s = new_offsets[:, 0]
        y1s = new_offsets[:, 1]
        x2s = x1s + self._crop_shapes[:, 1] - 1
        y2s = y1s + self._crop_shapes[:, 0] - 1

        needs_clip: npt.NDArray[np.bool_] = (
            (x1s < 0) | (y1s < 0) | (x2s >= new_w) | (y2s >= new_h)
        )

        if not needs_clip.any():
            # Fast path: pure offset arithmetic, no decode/re-encode needed.
            return CompactMask(
                list(self._rles),
                self._crop_shapes.copy(),
                new_offsets,
                new_image_shape,
            )

        # Slow path: only decode+clip+re-encode the masks that actually overflow.
        out_rles: list[npt.NDArray[np.int32]] = []
        out_crop_shapes: list[tuple[int, int]] = []
        out_offsets_list: list[tuple[int, int]] = []

        for mask_idx in range(num_masks):
            x1 = int(x1s[mask_idx])
            y1 = int(y1s[mask_idx])
            x2 = int(x2s[mask_idx])
            y2 = int(y2s[mask_idx])

            if not needs_clip[mask_idx]:
                out_rles.append(self._rles[mask_idx])
                out_crop_shapes.append(
                    (
                        int(self._crop_shapes[mask_idx, 0]),
                        int(self._crop_shapes[mask_idx, 1]),
                    )
                )
                out_offsets_list.append((x1, y1))
                continue

            ix1 = max(0, x1)
            iy1 = max(0, y1)
            ix2 = min(new_w - 1, x2)
            iy2 = min(new_h - 1, y2)

            if ix1 > ix2 or iy1 > iy2:
                anchor_x = min(max(x1, 0), new_w - 1)
                anchor_y = min(max(y1, 0), new_h - 1)
                out_rles.append(_mask_to_rle_counts(np.zeros((1, 1), dtype=bool)))
                out_crop_shapes.append((1, 1))
                out_offsets_list.append((anchor_x, anchor_y))
                continue

            crop = self.crop(mask_idx)
            clipped = crop[iy1 - y1 : iy2 - y1 + 1, ix1 - x1 : ix2 - x1 + 1]
            out_rles.append(_mask_to_rle_counts(clipped))
            out_crop_shapes.append((iy2 - iy1 + 1, ix2 - ix1 + 1))
            out_offsets_list.append((ix1, iy1))

        return CompactMask(
            out_rles,
            np.array(out_crop_shapes, dtype=np.int32),
            np.array(out_offsets_list, dtype=np.int32),
            new_image_shape,
        )

    # ------------------------------------------------------------------
    # Resize
    # ------------------------------------------------------------------

    def resize(self, new_image_shape: tuple[int, int]) -> CompactMask:
        """Return a new CompactMask scaled to a different image resolution.

        Each crop mask is resized with nearest-neighbour interpolation.
        Sparse masks use direct RLE arithmetic (:func:`_rle_resize`); dense
        masks fall back to ``cv2.resize(INTER_NEAREST)``.  Offsets and crop
        dimensions are scaled proportionally to the new image size.

        Performance notes:

        * Coordinate arithmetic is fully vectorised (no Python loop over N).
        * All-``False`` crops skip decode/resize entirely.
        * For N >= 8, resize runs in a thread pool — NumPy and OpenCV
          release the GIL so crops execute in parallel on multi-core CPUs.

        Args:
            new_image_shape: ``(H, W)`` of the target image.

        Returns:
            New :class:`CompactMask` with updated ``image_shape``, scaled
            offsets, scaled crop shapes, and re-encoded RLE crops.

        Raises:
            ValueError: If any dimension in *new_image_shape* is ``<= 0``.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> from supervision.detection.compact_mask import CompactMask
            >>> masks = np.zeros((1, 100, 100), dtype=bool)
            >>> masks[0, 20:40, 30:60] = True
            >>> xyxy = np.array([[30, 20, 59, 39]], dtype=np.float32)
            >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
            >>> small = cm.resize((50, 50))
            >>> small.shape
            (1, 50, 50)
            >>> small.offsets[0].tolist()
            [15, 10]

            ```
        """
        from concurrent.futures import ThreadPoolExecutor

        new_h, new_w = new_image_shape
        if new_h <= 0 or new_w <= 0:
            raise ValueError("new_image_shape must contain positive dimensions")

        # fast path — identity resize; list() creates a new container but the
        # individual RLE numpy arrays are shared (shallow copy).  Callers must
        # not mutate returned RLE arrays in-place.
        if (new_h, new_w) == self._image_shape:
            return CompactMask(
                list(self._rles),
                self._crop_shapes.copy(),
                self._offsets.copy(),
                new_image_shape,
            )

        # empty guard
        if len(self) == 0:
            return CompactMask(
                [],
                np.empty((0, 2), dtype=np.int32),
                np.empty((0, 2), dtype=np.int32),
                new_image_shape,
            )

        img_h, img_w = self._image_shape
        sx = new_w / img_w
        sy = new_h / img_h

        # L1 — vectorised coordinate arithmetic; no Python loop over N masks.
        x1s = self._offsets[:, 0].astype(np.float64)
        y1s = self._offsets[:, 1].astype(np.float64)
        x2s = x1s + self._crop_shapes[:, 1] - 1  # inclusive right edge
        y2s = y1s + self._crop_shapes[:, 0] - 1  # inclusive bottom edge

        new_x1s = np.clip(np.round(x1s * sx), 0, new_w - 1).astype(np.int32)
        new_y1s = np.clip(np.round(y1s * sy), 0, new_h - 1).astype(np.int32)
        new_x2s = np.clip(np.round(x2s * sx), 0, new_w - 1).astype(np.int32)
        new_y2s = np.clip(np.round(y2s * sy), 0, new_h - 1).astype(np.int32)
        new_crop_ws: npt.NDArray[np.int32] = np.maximum(
            1, new_x2s - new_x1s + 1
        ).astype(np.int32)
        new_crop_hs: npt.NDArray[np.int32] = np.maximum(
            1, new_y2s - new_y1s + 1
        ).astype(np.int32)

        # L2b — parallel per-crop resize; NumPy and OpenCV release the GIL.
        orig_crop_hs = self._crop_shapes[:, 0]
        orig_crop_ws = self._crop_shapes[:, 1]

        args = [
            (
                self._rles[i],
                int(orig_crop_hs[i]),
                int(orig_crop_ws[i]),
                int(new_crop_hs[i]),
                int(new_crop_ws[i]),
            )
            for i in range(len(self))
        ]

        n = len(self)
        if n >= _PARALLEL_THRESHOLD:
            with ThreadPoolExecutor(max_workers=min(n, os.cpu_count() or 4)) as pool:
                new_rles: list[npt.NDArray[np.int32]] = list(
                    pool.map(lambda a: _resize_crop(*a), args)
                )
        else:
            new_rles = [_resize_crop(*a) for a in args]

        new_crop_shapes = np.column_stack((new_crop_hs, new_crop_ws)).astype(np.int32)
        new_offsets = np.column_stack((new_x1s, new_y1s)).astype(np.int32)
        return CompactMask(new_rles, new_crop_shapes, new_offsets, new_image_shape)

Attributes

area: npt.NDArray[np.int64] property

Compute the area (True pixel count) of each mask.

Returns:

Type Description
NDArray[int64]

int64 array of shape (N,) with per-mask pixel counts.

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((2, 100, 100), dtype=bool)
>>> masks[0, 0:10, 0:10] = True  # 100 pixels
>>> masks[1, 0:5, 0:5] = True    # 25 pixels
>>> xyxy = np.array([[0, 0, 9, 9], [0, 0, 4, 4]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
>>> cm.area.tolist()
[100, 25]

bbox_xyxy: npt.NDArray[np.int32] property

Return per-mask inclusive bounding boxes in xyxy format.

Boxes are derived from crop metadata: x2 = x1 + crop_w - 1, y2 = y1 + crop_h - 1.

Returns:

Type Description
NDArray[int32]

Array of shape (N, 4) with int32 boxes

NDArray[int32]

[x1, y1, x2, y2].

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> masks[0, 2:5, 3:7] = True
>>> xyxy = np.array([[3, 2, 6, 4]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> cm.bbox_xyxy.tolist()
[[3, 2, 6, 4]]

dtype: np.dtype[Any] property

Return np.dtype(bool) — always.

Returns:

Type Description
dtype[Any]

np.dtype(bool).

Examples:

>>> from supervision.detection.compact_mask import CompactMask
>>> import numpy as np
>>> cm = CompactMask(
...     [], np.empty((0, 2), dtype=np.int32),
...     np.empty((0, 2), dtype=np.int32), (100, 100))
>>> cm.dtype
dtype('bool')

offsets: npt.NDArray[np.int32] property

Return per-mask crop origins as (x1, y1) integer offsets.

Returns:

Type Description
NDArray[int32]

Array of shape (N, 2) with int32 offsets.

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> masks[0, 2:4, 3:5] = True
>>> xyxy = np.array([[3, 2, 4, 3]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> cm.offsets.tolist()
[[3, 2]]

shape: tuple[int, int, int] property

Return (N, H, W) matching the dense mask convention.

Returns:

Type Description
tuple[int, int, int]

Tuple (N, H, W).

Examples:

>>> from supervision.detection.compact_mask import CompactMask
>>> import numpy as np
>>> cm = CompactMask(
...     [], np.empty((0, 2), dtype=np.int32),
...     np.empty((0, 2), dtype=np.int32), (480, 640))
>>> cm.shape
(0, 480, 640)

Functions

__array__(dtype: np.dtype[Any] | None = None) -> npt.NDArray[Any]

NumPy interop: materialise as a dense (N, H, W) array.

Called by np.asarray(compact_mask) and similar NumPy functions.

Parameters:

Name Type Description Default
dtype
dtype[Any] | None

Optional dtype to cast the result to.

None

Returns:

Type Description
NDArray[Any]

Dense boolean array of shape (N, H, W).

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> xyxy = np.array([[0, 0, 5, 5]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> np.asarray(cm).shape
(1, 10, 10)
Source code in src/supervision/detection/compact_mask.py
def __array__(self, dtype: np.dtype[Any] | None = None) -> npt.NDArray[Any]:
    """NumPy interop: materialise as a dense ``(N, H, W)`` array.

    Called by ``np.asarray(compact_mask)`` and similar NumPy functions.

    Args:
        dtype: Optional dtype to cast the result to.

    Returns:
        Dense boolean array of shape ``(N, H, W)``.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((1, 10, 10), dtype=bool)
        >>> xyxy = np.array([[0, 0, 5, 5]], dtype=np.float32)
        >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
        >>> np.asarray(cm).shape
        (1, 10, 10)

        ```
    """
    result = self.to_dense()
    if dtype is not None:
        return result.astype(dtype)
    return result

__eq__(other: object) -> bool

Element-wise equality with another :class:CompactMask or ndarray.

Parameters:

Name Type Description Default
other
object

Another :class:CompactMask or np.ndarray.

required

Returns:

Type Description
bool

True if all masks are pixel-identical.

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> xyxy = np.array([[0, 0, 5, 5]], dtype=np.float32)
>>> cm1 = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> cm2 = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> cm1 == cm2
True
Source code in src/supervision/detection/compact_mask.py
def __eq__(self, other: object) -> bool:
    """Element-wise equality with another :class:`CompactMask` or ndarray.

    Args:
        other: Another :class:`CompactMask` or ``np.ndarray``.

    Returns:
        ``True`` if all masks are pixel-identical.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((1, 10, 10), dtype=bool)
        >>> xyxy = np.array([[0, 0, 5, 5]], dtype=np.float32)
        >>> cm1 = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
        >>> cm2 = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
        >>> cm1 == cm2
        True

        ```
    """
    if isinstance(other, CompactMask):
        return bool(np.array_equal(self.to_dense(), other.to_dense()))
    if isinstance(other, np.ndarray):
        return bool(np.array_equal(self.to_dense(), other))
    return NotImplemented

__getitem__(index: int | slice | list[Any] | npt.NDArray[Any]) -> npt.NDArray[np.bool_] | CompactMask

Index into the mask collection.

  • int → dense (H, W) bool array (for annotators, iterators).
  • slice | list | ndarray → new :class:CompactMask (for filtering).

Parameters:

Name Type Description Default
index
int | slice | list[Any] | NDArray[Any]

An integer returns a dense (H, W) mask. Any other supported index type returns a new :class:CompactMask.

required

Returns:

Type Description
NDArray[bool_] | CompactMask

Dense (H, W) np.ndarray for integer index, or a new

NDArray[bool_] | CompactMask

class:CompactMask for all other index types.

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((3, 20, 20), dtype=bool)
>>> xyxy = np.array(
...     [[0,0,5,5],[5,5,10,10],[10,10,15,15]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(20, 20))
>>> cm[0].shape        # int → dense (H, W)
(20, 20)
>>> len(cm[[0, 2]])    # list → CompactMask
2
Source code in src/supervision/detection/compact_mask.py
def __getitem__(
    self,
    index: int | slice | list[Any] | npt.NDArray[Any],
) -> npt.NDArray[np.bool_] | CompactMask:
    """Index into the mask collection.

    * ``int`` → dense ``(H, W)`` bool array (for annotators, iterators).
    * ``slice | list | ndarray`` → new :class:`CompactMask` (for filtering).

    Args:
        index: An integer returns a dense ``(H, W)`` mask.  Any other
            supported index type returns a new :class:`CompactMask`.

    Returns:
        Dense ``(H, W)`` ``np.ndarray`` for integer index, or a new
        :class:`CompactMask` for all other index types.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((3, 20, 20), dtype=bool)
        >>> xyxy = np.array(
        ...     [[0,0,5,5],[5,5,10,10],[10,10,15,15]], dtype=np.float32)
        >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(20, 20))
        >>> cm[0].shape        # int → dense (H, W)
        (20, 20)
        >>> len(cm[[0, 2]])    # list → CompactMask
        2

        ```
    """
    if isinstance(index, (int, np.integer)):
        idx = int(index)
        img_h, img_w = self._image_shape
        result: npt.NDArray[np.bool_] = np.zeros((img_h, img_w), dtype=bool)
        crop_h = int(self._crop_shapes[idx, 0])
        crop_w = int(self._crop_shapes[idx, 1])
        x1 = int(self._offsets[idx, 0])
        y1 = int(self._offsets[idx, 1])
        crop = _rle_counts_to_mask(self._rles[idx], crop_h, crop_w)
        result[y1 : y1 + crop_h, x1 : x1 + crop_w] = crop
        return result

    # Slice: use direct Python list slice and numpy view — O(k), no arange.
    if isinstance(index, slice):
        return CompactMask(
            self._rles[index],
            self._crop_shapes[index],
            self._offsets[index],
            self._image_shape,
        )

    # Boolean selectors and fancy index → convert to integer positions first.
    if isinstance(index, np.ndarray) and index.dtype == bool:
        idx_arr = np.where(index)[0]
    elif isinstance(index, list) and all(
        isinstance(item, (bool, np.bool_)) for item in index
    ):
        idx_arr = np.flatnonzero(np.asarray(index, dtype=bool))
    else:
        idx_arr = np.asarray(list(index), dtype=np.intp)

    new_rles = [self._rles[int(mask_idx)] for mask_idx in idx_arr]
    new_crop_shapes: npt.NDArray[np.int32] = self._crop_shapes[idx_arr]
    new_offsets: npt.NDArray[np.int32] = self._offsets[idx_arr]
    return CompactMask(new_rles, new_crop_shapes, new_offsets, self._image_shape)

__iter__() -> Iterator[npt.NDArray[np.bool_]]

Iterate over masks as dense (H, W) boolean arrays.

Source code in src/supervision/detection/compact_mask.py
def __iter__(self) -> Iterator[npt.NDArray[np.bool_]]:
    """Iterate over masks as dense ``(H, W)`` boolean arrays."""
    for mask_idx in range(len(self)):
        yield self[mask_idx]

__len__() -> int

Return the number of masks.

Returns:

Type Description
int

Number of masks N.

Examples:

>>> from supervision.detection.compact_mask import CompactMask
>>> import numpy as np
>>> cm = CompactMask(
...     [], np.empty((0, 2), dtype=np.int32),
...     np.empty((0, 2), dtype=np.int32), (100, 100))
>>> len(cm)
0
Source code in src/supervision/detection/compact_mask.py
def __len__(self) -> int:
    """Return the number of masks.

    Returns:
        Number of masks N.

    Examples:
        ```pycon
        >>> from supervision.detection.compact_mask import CompactMask
        >>> import numpy as np
        >>> cm = CompactMask(
        ...     [], np.empty((0, 2), dtype=np.int32),
        ...     np.empty((0, 2), dtype=np.int32), (100, 100))
        >>> len(cm)
        0

        ```
    """
    return len(self._rles)

crop(index: int) -> npt.NDArray[np.bool_]

Decode a single mask crop without allocating the full image array.

This is an O(crop_area) operation — ideal for annotators that only need the cropped region.

Parameters:

Name Type Description Default
index
int

Index of the mask to decode.

required

Returns:

Type Description
NDArray[bool_]

Boolean array of shape (crop_h, crop_w).

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 100, 100), dtype=bool)
>>> masks[0, 20:30, 10:40] = True
>>> xyxy = np.array([[10, 20, 39, 29]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
>>> cm.crop(0).shape
(10, 30)
Source code in src/supervision/detection/compact_mask.py
def crop(self, index: int) -> npt.NDArray[np.bool_]:
    """Decode a single mask crop without allocating the full image array.

    This is an O(crop_area) operation — ideal for annotators that only
    need the cropped region.

    Args:
        index: Index of the mask to decode.

    Returns:
        Boolean array of shape ``(crop_h, crop_w)``.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((1, 100, 100), dtype=bool)
        >>> masks[0, 20:30, 10:40] = True
        >>> xyxy = np.array([[10, 20, 39, 29]], dtype=np.float32)
        >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
        >>> cm.crop(0).shape
        (10, 30)

        ```
    """
    crop_h = int(self._crop_shapes[index, 0])
    crop_w = int(self._crop_shapes[index, 1])
    return _rle_counts_to_mask(self._rles[index], crop_h, crop_w)

from_dense(masks: npt.NDArray[np.bool_], xyxy: npt.NDArray[Any], image_shape: tuple[int, int]) -> CompactMask classmethod

Create a :class:CompactMask from a dense (N, H, W) bool array.

Bounding boxes are clipped to image bounds and interpreted in the supervision xyxy convention (inclusive max coordinates). A box with invalid ordering (x2 < x1 or y2 < y1) is replaced by a 1x1 all-False crop to avoid degenerate RLE.

Parameters:

Name Type Description Default
masks
NDArray[bool_]

Dense boolean mask array of shape (N, H, W).

required
xyxy
NDArray[Any]

Bounding boxes of shape (N, 4) in [x1, y1, x2, y2] format.

required
image_shape
tuple[int, int]

(H, W) of the full image.

required

Returns:

Type Description
CompactMask

A new :class:CompactMask instance.

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 100, 100), dtype=bool)
>>> masks[0, 10:20, 10:20] = True
>>> xyxy = np.array([[10, 10, 19, 19]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
>>> cm.shape
(1, 100, 100)
Source code in src/supervision/detection/compact_mask.py
@classmethod
def from_dense(
    cls,
    masks: npt.NDArray[np.bool_],
    xyxy: npt.NDArray[Any],
    image_shape: tuple[int, int],
) -> CompactMask:
    """Create a :class:`CompactMask` from a dense ``(N, H, W)`` bool array.

    Bounding boxes are clipped to image bounds and interpreted in the
    supervision ``xyxy`` convention (inclusive max coordinates). A
    box with invalid ordering (``x2 < x1`` or ``y2 < y1``) is replaced by
    a ``1x1`` all-False crop to avoid degenerate RLE.

    Args:
        masks: Dense boolean mask array of shape ``(N, H, W)``.
        xyxy: Bounding boxes of shape ``(N, 4)`` in ``[x1, y1, x2, y2]``
            format.
        image_shape: ``(H, W)`` of the full image.

    Returns:
        A new :class:`CompactMask` instance.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((1, 100, 100), dtype=bool)
        >>> masks[0, 10:20, 10:20] = True
        >>> xyxy = np.array([[10, 10, 19, 19]], dtype=np.float32)
        >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
        >>> cm.shape
        (1, 100, 100)

        ```
    """
    img_h, img_w = image_shape
    num_masks = len(masks)

    if num_masks == 0:
        return cls(
            [],
            np.empty((0, 2), dtype=np.int32),
            np.empty((0, 2), dtype=np.int32),
            image_shape,
        )

    rles: list[npt.NDArray[np.int32]] = []
    crop_shapes_list: list[tuple[int, int]] = []
    offsets_list: list[tuple[int, int]] = []

    for mask_idx in range(num_masks):
        x1, y1, x2, y2 = xyxy[mask_idx]
        x1c = int(max(0, min(int(x1), img_w - 1)))
        y1c = int(max(0, min(int(y1), img_h - 1)))
        x2c = int(max(0, min(int(x2), img_w - 1)))
        y2c = int(max(0, min(int(y2), img_h - 1)))
        crop: npt.NDArray[np.bool_]

        # supervision xyxy uses inclusive max coords, so slicing must add +1.
        if x2c < x1c or y2c < y1c:
            crop = np.zeros((1, 1), dtype=bool)
            x2c, y2c = x1c, y1c
        else:
            crop = masks[mask_idx, y1c : y2c + 1, x1c : x2c + 1]

        crop_h = y2c - y1c + 1
        crop_w = x2c - x1c + 1
        rles.append(_mask_to_rle_counts(crop))
        crop_shapes_list.append((crop_h, crop_w))
        offsets_list.append((x1c, y1c))

    crop_shapes = np.array(crop_shapes_list, dtype=np.int32)
    offsets = np.array(offsets_list, dtype=np.int32)
    return cls(rles, crop_shapes, offsets, image_shape)

merge(masks_list: list[CompactMask]) -> CompactMask staticmethod

Concatenate multiple :class:CompactMask objects into one.

All inputs must have the same image_shape.

Parameters:

Name Type Description Default
masks_list
list[CompactMask]

Non-empty list of :class:CompactMask objects.

required

Returns:

Type Description
CompactMask

A new :class:CompactMask containing every mask from the inputs,

CompactMask

in order.

Raises:

Type Description
ValueError

If masks_list is empty or image shapes differ.

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks1 = np.zeros((2, 50, 50), dtype=bool)
>>> masks2 = np.zeros((3, 50, 50), dtype=bool)
>>> xyxy1 = np.array([[0,0,10,10],[10,10,20,20]], dtype=np.float32)
>>> xyxy2 = np.array(
...     [[0,0,5,5],[5,5,10,10],[10,10,15,15]], dtype=np.float32)
>>> cm1 = CompactMask.from_dense(masks1, xyxy1, image_shape=(50, 50))
>>> cm2 = CompactMask.from_dense(masks2, xyxy2, image_shape=(50, 50))
>>> len(CompactMask.merge([cm1, cm2]))
5
Source code in src/supervision/detection/compact_mask.py
@staticmethod
def merge(masks_list: list[CompactMask]) -> CompactMask:
    """Concatenate multiple :class:`CompactMask` objects into one.

    All inputs must have the same ``image_shape``.

    Args:
        masks_list: Non-empty list of :class:`CompactMask` objects.

    Returns:
        A new :class:`CompactMask` containing every mask from the inputs,
        in order.

    Raises:
        ValueError: If ``masks_list`` is empty or image shapes differ.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks1 = np.zeros((2, 50, 50), dtype=bool)
        >>> masks2 = np.zeros((3, 50, 50), dtype=bool)
        >>> xyxy1 = np.array([[0,0,10,10],[10,10,20,20]], dtype=np.float32)
        >>> xyxy2 = np.array(
        ...     [[0,0,5,5],[5,5,10,10],[10,10,15,15]], dtype=np.float32)
        >>> cm1 = CompactMask.from_dense(masks1, xyxy1, image_shape=(50, 50))
        >>> cm2 = CompactMask.from_dense(masks2, xyxy2, image_shape=(50, 50))
        >>> len(CompactMask.merge([cm1, cm2]))
        5

        ```
    """
    if not masks_list:
        raise ValueError("Cannot merge an empty list of CompactMask objects.")

    image_shape = masks_list[0]._image_shape
    for cm in masks_list[1:]:
        if cm._image_shape != image_shape:
            raise ValueError(
                f"Cannot merge CompactMask objects with different image shapes: "
                f"{image_shape} vs {cm._image_shape}"
            )

    # list.extend is a C-level call and avoids the per-element Python
    # bytecode overhead of a flat list comprehension.  This matters under
    # GIL contention when multiple threads call merge concurrently.
    new_rles: list[npt.NDArray[np.int32]] = []
    for cm in masks_list:
        new_rles.extend(cm._rles)

    # np.concatenate handles (0, 2) arrays correctly.
    # No .astype() needed — _crop_shapes and _offsets are already int32.
    new_crop_shapes: npt.NDArray[np.int32] = np.concatenate(
        [cm._crop_shapes for cm in masks_list], axis=0
    )
    new_offsets: npt.NDArray[np.int32] = np.concatenate(
        [cm._offsets for cm in masks_list], axis=0
    )

    return CompactMask(new_rles, new_crop_shapes, new_offsets, image_shape)

repack() -> CompactMask

Re-encode all masks using tight bounding boxes.

When the original xyxy boxes are padded or loose — common with object-detector outputs and full-image boxes used in tests — each RLE crop encodes more background (False) pixels than necessary. This method decodes every crop, trims it to the minimal rectangle that contains all True pixels, and re-encodes. All-False masks are normalised to a 1x1 all-False crop.

The call is O(sum of crop areas) — suitable as a one-time cleanup after accumulating many merges (e.g. after :class:~supervision.detection.tools.inference_slicer.InferenceSlicer tiles are merged).

Returns:

Type Description
CompactMask

A new :class:CompactMask with minimal-area crops and updated

CompactMask

offsets.

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> masks[0, 3:7, 3:7] = True
>>> # Deliberately loose bbox: covers the full image.
>>> xyxy = np.array([[0, 0, 9, 9]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> repacked = cm.repack()
>>> repacked.offsets.tolist()  # tight origin: x1=3, y1=3
[[3, 3]]
Source code in src/supervision/detection/compact_mask.py
def repack(self) -> CompactMask:
    """Re-encode all masks using tight bounding boxes.

    When the original ``xyxy`` boxes are padded or loose — common with
    object-detector outputs and full-image boxes used in tests — each RLE
    crop encodes more background (``False``) pixels than necessary.  This
    method decodes every crop, trims it to the minimal rectangle that
    contains all ``True`` pixels, and re-encodes.  All-``False`` masks are
    normalised to a ``1x1`` all-``False`` crop.

    The call is O(sum of crop areas) — suitable as a one-time cleanup
    after accumulating many merges (e.g. after
    :class:`~supervision.detection.tools.inference_slicer.InferenceSlicer`
    tiles are merged).

    Returns:
        A new :class:`CompactMask` with minimal-area crops and updated
        offsets.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((1, 10, 10), dtype=bool)
        >>> masks[0, 3:7, 3:7] = True
        >>> # Deliberately loose bbox: covers the full image.
        >>> xyxy = np.array([[0, 0, 9, 9]], dtype=np.float32)
        >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
        >>> repacked = cm.repack()
        >>> repacked.offsets.tolist()  # tight origin: x1=3, y1=3
        [[3, 3]]

        ```
    """
    num_masks = len(self._rles)
    if num_masks == 0:
        return CompactMask(
            [],
            np.empty((0, 2), dtype=np.int32),
            np.empty((0, 2), dtype=np.int32),
            self._image_shape,
        )

    new_rles: list[npt.NDArray[np.int32]] = []
    new_crop_shapes_list: list[tuple[int, int]] = []
    new_offsets_list: list[tuple[int, int]] = []

    for mask_idx in range(num_masks):
        crop = self.crop(mask_idx)
        x1_off = int(self._offsets[mask_idx, 0])
        y1_off = int(self._offsets[mask_idx, 1])

        rows_any = np.any(crop, axis=1)
        cols_any = np.any(crop, axis=0)

        if not rows_any.any():
            # All-False: normalise to 1x1 to avoid zero-sized arrays.
            new_rles.append(_mask_to_rle_counts(np.zeros((1, 1), dtype=bool)))
            new_crop_shapes_list.append((1, 1))
            new_offsets_list.append((x1_off, y1_off))
            continue

        y_indices = np.where(rows_any)[0]
        x_indices = np.where(cols_any)[0]
        y_min, y_max = int(y_indices[0]), int(y_indices[-1])
        x_min, x_max = int(x_indices[0]), int(x_indices[-1])

        tight = crop[y_min : y_max + 1, x_min : x_max + 1]
        new_rles.append(_mask_to_rle_counts(tight))
        new_crop_shapes_list.append((y_max - y_min + 1, x_max - x_min + 1))
        new_offsets_list.append((x1_off + x_min, y1_off + y_min))

    return CompactMask(
        new_rles,
        np.array(new_crop_shapes_list, dtype=np.int32),
        np.array(new_offsets_list, dtype=np.int32),
        self._image_shape,
    )

resize(new_image_shape: tuple[int, int]) -> CompactMask

Return a new CompactMask scaled to a different image resolution.

Each crop mask is resized with nearest-neighbour interpolation. Sparse masks use direct RLE arithmetic (:func:_rle_resize); dense masks fall back to cv2.resize(INTER_NEAREST). Offsets and crop dimensions are scaled proportionally to the new image size.

Performance notes:

  • Coordinate arithmetic is fully vectorised (no Python loop over N).
  • All-False crops skip decode/resize entirely.
  • For N >= 8, resize runs in a thread pool — NumPy and OpenCV release the GIL so crops execute in parallel on multi-core CPUs.

Parameters:

Name Type Description Default
new_image_shape
tuple[int, int]

(H, W) of the target image.

required

Returns:

Name Type Description
New CompactMask

class:CompactMask with updated image_shape, scaled

CompactMask

offsets, scaled crop shapes, and re-encoded RLE crops.

Raises:

Type Description
ValueError

If any dimension in new_image_shape is <= 0.

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 100, 100), dtype=bool)
>>> masks[0, 20:40, 30:60] = True
>>> xyxy = np.array([[30, 20, 59, 39]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
>>> small = cm.resize((50, 50))
>>> small.shape
(1, 50, 50)
>>> small.offsets[0].tolist()
[15, 10]
Source code in src/supervision/detection/compact_mask.py
def resize(self, new_image_shape: tuple[int, int]) -> CompactMask:
    """Return a new CompactMask scaled to a different image resolution.

    Each crop mask is resized with nearest-neighbour interpolation.
    Sparse masks use direct RLE arithmetic (:func:`_rle_resize`); dense
    masks fall back to ``cv2.resize(INTER_NEAREST)``.  Offsets and crop
    dimensions are scaled proportionally to the new image size.

    Performance notes:

    * Coordinate arithmetic is fully vectorised (no Python loop over N).
    * All-``False`` crops skip decode/resize entirely.
    * For N >= 8, resize runs in a thread pool — NumPy and OpenCV
      release the GIL so crops execute in parallel on multi-core CPUs.

    Args:
        new_image_shape: ``(H, W)`` of the target image.

    Returns:
        New :class:`CompactMask` with updated ``image_shape``, scaled
        offsets, scaled crop shapes, and re-encoded RLE crops.

    Raises:
        ValueError: If any dimension in *new_image_shape* is ``<= 0``.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((1, 100, 100), dtype=bool)
        >>> masks[0, 20:40, 30:60] = True
        >>> xyxy = np.array([[30, 20, 59, 39]], dtype=np.float32)
        >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
        >>> small = cm.resize((50, 50))
        >>> small.shape
        (1, 50, 50)
        >>> small.offsets[0].tolist()
        [15, 10]

        ```
    """
    from concurrent.futures import ThreadPoolExecutor

    new_h, new_w = new_image_shape
    if new_h <= 0 or new_w <= 0:
        raise ValueError("new_image_shape must contain positive dimensions")

    # fast path — identity resize; list() creates a new container but the
    # individual RLE numpy arrays are shared (shallow copy).  Callers must
    # not mutate returned RLE arrays in-place.
    if (new_h, new_w) == self._image_shape:
        return CompactMask(
            list(self._rles),
            self._crop_shapes.copy(),
            self._offsets.copy(),
            new_image_shape,
        )

    # empty guard
    if len(self) == 0:
        return CompactMask(
            [],
            np.empty((0, 2), dtype=np.int32),
            np.empty((0, 2), dtype=np.int32),
            new_image_shape,
        )

    img_h, img_w = self._image_shape
    sx = new_w / img_w
    sy = new_h / img_h

    # L1 — vectorised coordinate arithmetic; no Python loop over N masks.
    x1s = self._offsets[:, 0].astype(np.float64)
    y1s = self._offsets[:, 1].astype(np.float64)
    x2s = x1s + self._crop_shapes[:, 1] - 1  # inclusive right edge
    y2s = y1s + self._crop_shapes[:, 0] - 1  # inclusive bottom edge

    new_x1s = np.clip(np.round(x1s * sx), 0, new_w - 1).astype(np.int32)
    new_y1s = np.clip(np.round(y1s * sy), 0, new_h - 1).astype(np.int32)
    new_x2s = np.clip(np.round(x2s * sx), 0, new_w - 1).astype(np.int32)
    new_y2s = np.clip(np.round(y2s * sy), 0, new_h - 1).astype(np.int32)
    new_crop_ws: npt.NDArray[np.int32] = np.maximum(
        1, new_x2s - new_x1s + 1
    ).astype(np.int32)
    new_crop_hs: npt.NDArray[np.int32] = np.maximum(
        1, new_y2s - new_y1s + 1
    ).astype(np.int32)

    # L2b — parallel per-crop resize; NumPy and OpenCV release the GIL.
    orig_crop_hs = self._crop_shapes[:, 0]
    orig_crop_ws = self._crop_shapes[:, 1]

    args = [
        (
            self._rles[i],
            int(orig_crop_hs[i]),
            int(orig_crop_ws[i]),
            int(new_crop_hs[i]),
            int(new_crop_ws[i]),
        )
        for i in range(len(self))
    ]

    n = len(self)
    if n >= _PARALLEL_THRESHOLD:
        with ThreadPoolExecutor(max_workers=min(n, os.cpu_count() or 4)) as pool:
            new_rles: list[npt.NDArray[np.int32]] = list(
                pool.map(lambda a: _resize_crop(*a), args)
            )
    else:
        new_rles = [_resize_crop(*a) for a in args]

    new_crop_shapes = np.column_stack((new_crop_hs, new_crop_ws)).astype(np.int32)
    new_offsets = np.column_stack((new_x1s, new_y1s)).astype(np.int32)
    return CompactMask(new_rles, new_crop_shapes, new_offsets, new_image_shape)

sum(axis: int | tuple[int, ...] | None = None) -> npt.NDArray[Any] | int

NumPy-compatible sum with a fast path for per-mask area.

When axis=(1, 2), returns the per-mask True-pixel count via :attr:area without materialising the full dense array.

Parameters:

Name Type Description Default
axis
int | tuple[int, ...] | None

Axis or axes to sum over.

None

Returns:

Type Description
NDArray[Any] | int

Sum result matching NumPy semantics.

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> masks[0, 0:3, 0:3] = True
>>> xyxy = np.array([[0, 0, 2, 2]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> cm.sum(axis=(1, 2)).tolist()
[9]
Source code in src/supervision/detection/compact_mask.py
def sum(self, axis: int | tuple[int, ...] | None = None) -> npt.NDArray[Any] | int:
    """NumPy-compatible sum with a fast path for per-mask area.

    When ``axis=(1, 2)``, returns the per-mask True-pixel count via
    :attr:`area` without materialising the full dense array.

    Args:
        axis: Axis or axes to sum over.

    Returns:
        Sum result matching NumPy semantics.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((1, 10, 10), dtype=bool)
        >>> masks[0, 0:3, 0:3] = True
        >>> xyxy = np.array([[0, 0, 2, 2]], dtype=np.float32)
        >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
        >>> cm.sum(axis=(1, 2)).tolist()
        [9]

        ```
    """
    if axis == (1, 2):
        return self.area
    return self.to_dense().sum(axis=axis)

to_dense() -> npt.NDArray[np.bool_]

Materialise all masks as a dense (N, H, W) boolean array.

Returns:

Type Description
NDArray[bool_]

Boolean array of shape (N, H, W).

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 50, 50), dtype=bool)
>>> masks[0, 10:20, 10:30] = True
>>> xyxy = np.array([[10, 10, 29, 19]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(50, 50))
>>> cm.to_dense().shape
(1, 50, 50)
Source code in src/supervision/detection/compact_mask.py
def to_dense(self) -> npt.NDArray[np.bool_]:
    """Materialise all masks as a dense ``(N, H, W)`` boolean array.

    Returns:
        Boolean array of shape ``(N, H, W)``.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((1, 50, 50), dtype=bool)
        >>> masks[0, 10:20, 10:30] = True
        >>> xyxy = np.array([[10, 10, 29, 19]], dtype=np.float32)
        >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(50, 50))
        >>> cm.to_dense().shape
        (1, 50, 50)

        ```
    """
    num_masks = len(self._rles)
    img_h, img_w = self._image_shape
    result: npt.NDArray[np.bool_] = np.zeros((num_masks, img_h, img_w), dtype=bool)
    for mask_idx in range(num_masks):
        crop_h, crop_w = (
            int(self._crop_shapes[mask_idx, 0]),
            int(self._crop_shapes[mask_idx, 1]),
        )
        x1, y1 = int(self._offsets[mask_idx, 0]), int(self._offsets[mask_idx, 1])
        crop = _rle_counts_to_mask(self._rles[mask_idx], crop_h, crop_w)
        result[mask_idx, y1 : y1 + crop_h, x1 : x1 + crop_w] = crop
    return result

with_offset(dx: int, dy: int, new_image_shape: tuple[int, int]) -> CompactMask

Return a new :class:CompactMask with adjusted offsets and image shape.

Used by :class:~supervision.detection.tools.inference_slicer.InferenceSlicer to relocate tile-local masks into full-image coordinates without materialising the dense (N, H, W) array.

Parameters:

Name Type Description Default
dx
int

Pixels to add to every mask's x1 offset.

required
dy
int

Pixels to add to every mask's y1 offset.

required
new_image_shape
tuple[int, int]

(H, W) of the full (destination) image.

required

Returns:

Name Type Description
New CompactMask

class:CompactMask with updated offsets and image shape.

CompactMask

Crops are clipped to stay inside new_image_shape; masks fully

CompactMask

outside are represented as 1x1 all-False crops.

Examples:

>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 20, 20), dtype=bool)
>>> xyxy = np.array([[5, 5, 15, 15]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(20, 20))
>>> cm2 = cm.with_offset(100, 200, new_image_shape=(400, 400))
>>> cm2.offsets[0].tolist()
[105, 205]
Source code in src/supervision/detection/compact_mask.py
def with_offset(
    self,
    dx: int,
    dy: int,
    new_image_shape: tuple[int, int],
) -> CompactMask:
    """Return a new :class:`CompactMask` with adjusted offsets and image shape.

    Used by :class:`~supervision.detection.tools.inference_slicer.InferenceSlicer`
    to relocate tile-local masks into full-image coordinates without
    materialising the dense ``(N, H, W)`` array.

    Args:
        dx: Pixels to add to every mask's ``x1`` offset.
        dy: Pixels to add to every mask's ``y1`` offset.
        new_image_shape: ``(H, W)`` of the full (destination) image.

    Returns:
        New :class:`CompactMask` with updated offsets and image shape.
        Crops are clipped to stay inside ``new_image_shape``; masks fully
        outside are represented as ``1x1`` all-False crops.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.detection.compact_mask import CompactMask
        >>> masks = np.zeros((1, 20, 20), dtype=bool)
        >>> xyxy = np.array([[5, 5, 15, 15]], dtype=np.float32)
        >>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(20, 20))
        >>> cm2 = cm.with_offset(100, 200, new_image_shape=(400, 400))
        >>> cm2.offsets[0].tolist()
        [105, 205]

        ```
    """
    new_h, new_w = new_image_shape
    if new_h <= 0 or new_w <= 0:
        raise ValueError("new_image_shape must contain positive dimensions")

    num_masks = len(self)
    if num_masks == 0:
        return CompactMask(
            [],
            np.empty((0, 2), dtype=np.int32),
            np.empty((0, 2), dtype=np.int32),
            new_image_shape,
        )

    # Vectorised bounds check: compute every new [x1,y1,x2,y2] at once.
    # For the common case (InferenceSlicer tiles that fit fully inside the
    # new canvas) this catches the "no clipping needed" path in O(N) numpy
    # without touching any RLE data.
    new_offsets: npt.NDArray[np.int32] = self._offsets + np.array(
        [dx, dy], dtype=np.int32
    )
    x1s = new_offsets[:, 0]
    y1s = new_offsets[:, 1]
    x2s = x1s + self._crop_shapes[:, 1] - 1
    y2s = y1s + self._crop_shapes[:, 0] - 1

    needs_clip: npt.NDArray[np.bool_] = (
        (x1s < 0) | (y1s < 0) | (x2s >= new_w) | (y2s >= new_h)
    )

    if not needs_clip.any():
        # Fast path: pure offset arithmetic, no decode/re-encode needed.
        return CompactMask(
            list(self._rles),
            self._crop_shapes.copy(),
            new_offsets,
            new_image_shape,
        )

    # Slow path: only decode+clip+re-encode the masks that actually overflow.
    out_rles: list[npt.NDArray[np.int32]] = []
    out_crop_shapes: list[tuple[int, int]] = []
    out_offsets_list: list[tuple[int, int]] = []

    for mask_idx in range(num_masks):
        x1 = int(x1s[mask_idx])
        y1 = int(y1s[mask_idx])
        x2 = int(x2s[mask_idx])
        y2 = int(y2s[mask_idx])

        if not needs_clip[mask_idx]:
            out_rles.append(self._rles[mask_idx])
            out_crop_shapes.append(
                (
                    int(self._crop_shapes[mask_idx, 0]),
                    int(self._crop_shapes[mask_idx, 1]),
                )
            )
            out_offsets_list.append((x1, y1))
            continue

        ix1 = max(0, x1)
        iy1 = max(0, y1)
        ix2 = min(new_w - 1, x2)
        iy2 = min(new_h - 1, y2)

        if ix1 > ix2 or iy1 > iy2:
            anchor_x = min(max(x1, 0), new_w - 1)
            anchor_y = min(max(y1, 0), new_h - 1)
            out_rles.append(_mask_to_rle_counts(np.zeros((1, 1), dtype=bool)))
            out_crop_shapes.append((1, 1))
            out_offsets_list.append((anchor_x, anchor_y))
            continue

        crop = self.crop(mask_idx)
        clipped = crop[iy1 - y1 : iy2 - y1 + 1, ix1 - x1 : ix2 - x1 + 1]
        out_rles.append(_mask_to_rle_counts(clipped))
        out_crop_shapes.append((iy2 - iy1 + 1, ix2 - ix1 + 1))
        out_offsets_list.append((ix1, iy1))

    return CompactMask(
        out_rles,
        np.array(out_crop_shapes, dtype=np.int32),
        np.array(out_offsets_list, dtype=np.int32),
        new_image_shape,
    )

Comments