Masks Utils¶
supervision.detection.utils.masks.move_masks(masks, offset, resolution_wh)
¶
Offset the masks in an array by the specified (x, y) amount.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
NDArray[bool_]
|
A 3D array of binary masks corresponding to the
predictions. Shape: |
required |
|
NDArray[int32]
|
An array of shape |
required |
|
Tuple[int, int]
|
The width and height of the desired mask resolution. |
required |
Returns:
Type | Description |
---|---|
NDArray[bool_]
|
(npt.NDArray[np.bool_]) repositioned masks, optionally padded to the specified shape. |
Examples:
import numpy as np
import supervision as sv
mask = np.array([[[False, False, False, False],
[False, True, True, False],
[False, True, True, False],
[False, False, False, False]]], dtype=bool)
offset = np.array([1, 1])
sv.move_masks(mask, offset, resolution_wh=(4, 4))
# array([[[False, False, False, False],
# [False, False, False, False],
# [False, False, True, True],
# [False, False, True, True]]], dtype=bool)
offset = np.array([-2, 2])
sv.move_masks(mask, offset, resolution_wh=(4, 4))
# array([[[False, False, False, False],
# [False, False, False, False],
# [False, False, False, False],
# [True, False, False, False]]], dtype=bool)
Source code in supervision/detection/utils/masks.py
supervision.detection.utils.masks.contains_holes(mask)
¶
Checks if the binary mask contains holes (background pixels fully enclosed by foreground pixels).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
NDArray[bool_]
|
2D binary mask where |
required |
Returns:
Type | Description |
---|---|
bool
|
True if holes are detected, False otherwise. |
Examples:
import numpy as np
import supervision as sv
mask = np.array([
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 0, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]
]).astype(bool)
sv.contains_holes(mask=mask)
# True
mask = np.array([
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]
]).astype(bool)
sv.contains_holes(mask=mask)
# False
Source code in supervision/detection/utils/masks.py
supervision.detection.utils.masks.contains_multiple_segments(mask, connectivity=4)
¶
Checks if the binary mask contains multiple unconnected foreground segments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
NDArray[bool_]
|
2D binary mask where |
required |
|
int)
|
Default: 4 is 4-way connectivity, which means that foreground pixels are the part of the same segment/component if their edges touch. Alternatively: 8 for 8-way connectivity, when foreground pixels are connected by their edges or corners touch. |
4
|
Returns:
Type | Description |
---|---|
bool
|
True when the mask contains multiple not connected components, False otherwise. |
Raises:
Type | Description |
---|---|
ValueError
|
If connectivity(int) parameter value is not 4 or 8. |
Examples:
import numpy as np
import supervision as sv
mask = np.array([
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 1, 1],
[0, 1, 1, 0, 1, 1],
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 0]
]).astype(bool)
sv.contains_multiple_segments(mask=mask, connectivity=4)
# True
mask = np.array([
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0]
]).astype(bool)
sv.contains_multiple_segments(mask=mask, connectivity=4)
# False