Datasets Utils¶
Converts run-length encoding (RLE) to a binary mask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Union[NDArray[int_], List[int]]
|
The 1D RLE array, the format used in the COCO dataset (column-wise encoding, values of an array with even indices represent the number of pixels assigned as background, values of an array with odd indices represent the number of pixels assigned as foreground object). |
required |
|
Tuple[int, int]
|
The width (w) and height (h) of the desired binary mask. |
required |
Returns:
Type | Description |
---|---|
NDArray[bool_]
|
The generated 2D Boolean mask of shape |
Raises:
Type | Description |
---|---|
AssertionError
|
If the sum of pixels encoded in RLE differs from the number of pixels in the expected mask (computed based on resolution_wh). |
Examples:
import supervision as sv
sv.rle_to_mask([5, 2, 2, 2, 5], (4, 4))
# array([
# [False, False, False, False],
# [False, True, True, False],
# [False, True, True, False],
# [False, False, False, False],
# ])
Source code in supervision/dataset/utils.py
Converts a binary mask into a run-length encoding (RLE).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
NDArray[bool_]
|
2D binary mask where |
required |
Returns:
Type | Description |
---|---|
List[int]
|
The run-length encoded mask. Values of a list with even indices
represent the number of pixels assigned as background ( |
Raises:
Type | Description |
---|---|
AssertionError
|
If input mask is not 2D or is empty. |
Examples:
import numpy as np
import supervision as sv
mask = np.array([
[True, True, True, True],
[True, True, True, True],
[True, True, True, True],
[True, True, True, True],
])
sv.mask_to_rle(mask)
# [0, 16]
mask = np.array([
[False, False, False, False],
[False, True, True, False],
[False, True, True, False],
[False, False, False, False],
])
sv.mask_to_rle(mask)
# [5, 2, 2, 2, 5]