Segmentation masks
Mask types
There are 3 ways a segmentation mask can be encoded in the annotations json file: Polygons
, RLE
or COCO_RLE
.
Examples of what each segmentation type looks like in the JSON file:
Polygons
: “segmentation”: [[510.66, 423.01, 511.72, 420.03, …, 510.45, 423.01]]RLE
: “segmentation”: {“size”: [40, 40], “counts”: [245, 5, 35, 5, …, 5, 35, 5, 1190]}COCO_RLE
: “segmentation”: {“size”: [480, 640], “counts”: “aUh2b0X…BgRU4”}
On top of those 3 segmentation types, this package introduces a fourth one called PolygonsRS
.
It follows the same format as the RLE
and COCO_RLE
types, but uses the polygons for the counts field:
PolygonsRS
: “segmentation”: {“size”: [480, 640], “counts”: [[510.66, 423.01, 511.72, 420.03, …, 510.45, 423.01]]}
The advantage of this format if that he polygons can be decoded into a mask of the same as the input image without having to look up its size. However it should not be written to a json file (as it is non-standard).
Decode masks
- rpycocotools.mask.decode(encoded_mask: RLE | COCO_RLE | Polygons | PolygonsRS, width: None | int, height: None | int) npt.NDArray[np.uint8]
Decode a mask to a
numpy.ndarray
.- Parameters:
encoded_mask (RLE | COCO_RLE | Polygons | PolygonsRS) – The encoded mask.
width (int) – Use only when the mask is a
Polygon
. The width of the image corresponding to the polygons.height (int) – Use only when the mask is a
Polygon
. The height of the image corresponding to the polygons.
- Raises:
ValueError – If the mask conversion failed.
- Returns:
The decoded mask as a NumPy array.
- Return type:
npt.NDArray[np.uint8]
Encode masks
- rpycocotools.mask.encode(mask: npt.NDArray[np.uint8], target: Literal['polygons', 'rle', 'coco_rle', 'polygons_rs']) Polygons | RLE | COCO_RLE | PolygonsRS:
Encode/compress a
numpy.ndarray
mask to the desired format.- Parameters:
mask (npt.NDArray[np.uint8]) – The uncompressed mask.
- Raises:
ValueError – If the mask conversion failed.
- Returns:
The compressed mask.
- Return type:
Polygons | RLE | COCO_RLE | PolygonsRS
Utils
- rpycocotools.mask.area(encoded_mask: RLE | COCO_RLE | PolygonsRS | Polygons) int:
Compute the area of the given mask.
- Parameters:
encoded_mask (RLE | COCO_RLE | PolygonsRS | Polygons) – The mask whose area should be computed.
- Returns:
The area
- Return type:
int
- rpycocotools.mask.to_bbox(encoded_mask: RLE | COCO_RLE | PolygonsRS | Polygons) rpycocotools.anns.BBox:
Compute the bounding box of the given mask.
- Parameters:
encoded_mask (RLE | COCO_RLE | PolygonsRS | Polygons) – The mask whose bounding box should be computed.
- Returns:
The bounding box
- Return type:
BBox