datumaro.util.mask_tools#

Functions

bgr2index(img)

check_is_mask(mask)

crop_covered_segments(segments, width, height)

Find all segments occluded by others and crop them to the visible part only.

extract_contours(mask)

Convert an instance mask to polygons

find_mask_bbox(mask)

generate_colormap([length, include_background])

Generates colors using PASCAL VOC algorithm.

index2bgr(id_map)

invert_colormap(colormap)

lazy_mask(path[, inverse_colormap])

load_mask(path[, inverse_colormap, default_id])

make_binary_mask(mask)

make_index_mask(binary_mask, index[, ...])

Create an index mask from a binary mask by filling a given index value.

mask_to_bboxes(mask)

Convert an instance mask to bboxes

mask_to_polygons(mask[, area_threshold])

Convert an instance mask to polygons

mask_to_rle(binary_mask)

mask_to_rle_py(binary_mask)

merge_masks(masks[, start])

Merges masks into one, mask order is responsible for z order.

paint_mask(mask[, colormap])

Applies colormap to index mask

remap_mask(mask, map_fn)

Changes mask elements from one colormap to another

rle_to_mask(rle_uncompressed)

Decode the uncompressed RLE string to the binary mask (2D np.ndarray)

rles_to_mask(rles, width, height)

unpaint_mask(painted_mask[, ...])

Convert color mask to index mask

datumaro.util.mask_tools.generate_colormap(length=256, *, include_background=True)[source]#

Generates colors using PASCAL VOC algorithm.

If include_background is True, the result will include the item “0: (0, 0, 0)”, which is typically used as a background color. Otherwise, indices will start from 0, but (0, 0, 0) is not included.

Returns index -> (R, G, B) mapping.

datumaro.util.mask_tools.invert_colormap(colormap)[source]#
datumaro.util.mask_tools.check_is_mask(mask)[source]#
datumaro.util.mask_tools.unpaint_mask(painted_mask, inverse_colormap=None, default_id=None)[source]#

Convert color mask to index mask

mask: HWC BGR [0; 255]

colormap: (R, G, B) -> index

datumaro.util.mask_tools.paint_mask(mask, colormap=None)[source]#

Applies colormap to index mask

mask: HW(C) [0; max_index] mask

colormap: index -> (R, G, B)

datumaro.util.mask_tools.remap_mask(mask, map_fn)[source]#

Changes mask elements from one colormap to another

# mask: HW(C) [0; max_index] mask

datumaro.util.mask_tools.make_index_mask(binary_mask: ndarray, index: int, ignore_index: int = 0, dtype: dtype | None = None)[source]#

Create an index mask from a binary mask by filling a given index value.

Parameters:
  • binary_mask – Binary mask to create an index mask.

  • index – Scalar value to fill the ones in the binary mask.

  • ignore_index – Scalar value to fill in the zeros in the binary mask. Defaults to 0.

  • dtype – Data type for the resulting mask. If not specified, it will be inferred from the provided index to hold its value. For example, if index=255, the inferred dtype will be np.uint8. Defaults to None.

Returns:

Index mask created from the binary mask.

Return type:

np.ndarray

Raises:

ValueError – If dtype is not specified and incompatible scalar types are used for index and ignore_index.

Examples

>>> binary_mask = np.eye(2, dtype=np.bool_)
>>> index_mask = make_index_mask(binary_mask, index=10, ignore_index=255, dtype=np.uint8)
>>> print(index_mask)
array([[ 10, 255],
       [255,  10]], dtype=uint8)
datumaro.util.mask_tools.make_binary_mask(mask)[source]#
datumaro.util.mask_tools.bgr2index(img)[source]#
datumaro.util.mask_tools.index2bgr(id_map)[source]#
datumaro.util.mask_tools.load_mask(path, inverse_colormap=None, default_id=None)[source]#
datumaro.util.mask_tools.lazy_mask(path, inverse_colormap=None)[source]#
datumaro.util.mask_tools.mask_to_rle(binary_mask)[source]#
datumaro.util.mask_tools.mask_to_rle_py(binary_mask)[source]#
datumaro.util.mask_tools.extract_contours(mask)[source]#

Convert an instance mask to polygons

Parameters:
  • mask – a 2d binary mask

  • tolerance – maximum distance from original points of a polygon to the approximated ones

  • area_threshold – minimal area of generated polygons

Returns:

A list of polygons like [[x1,y1, x2,y2 …], […]]

datumaro.util.mask_tools.mask_to_polygons(mask, area_threshold=1)[source]#

Convert an instance mask to polygons

Parameters:
  • mask – a 2d binary mask

  • tolerance – maximum distance from original points of a polygon to the approximated ones

  • area_threshold – minimal area of generated polygons

Returns:

A list of polygons like [[x1,y1, x2,y2 …], […]]

datumaro.util.mask_tools.mask_to_bboxes(mask)[source]#

Convert an instance mask to bboxes

Parameters:

mask – a 2d binary mask

Returns:

A list of bboxes like [[x1,x2,y1,y2], […]]

datumaro.util.mask_tools.crop_covered_segments(segments, width, height, iou_threshold=0.0, ratio_tolerance=0.001, area_threshold=1, return_masks=False)[source]#

Find all segments occluded by others and crop them to the visible part only. Input segments are expected to be sorted from background to foreground.

Parameters:
  • segments – 1d list of segment RLEs (in COCO format)

  • width – width of the image

  • height – height of the image

  • iou_threshold – IoU threshold for objects to be counted as intersected By default is set to 0 to process any intersected objects

  • ratio_tolerance – an IoU “handicap” value for a situation when an object is (almost) fully covered by another one and we don’t want make a “hole” in the background object

  • area_threshold – minimal area of included segments

Returns:

[
    [[x1,y1, x2,y2 ...], ...], # input segment #0 parts
    mask1, # input segment #1 mask (if source segment is mask)
    [], # when source segment is too small
    ...
]

Return type:

A list of input segments’ parts (in the same order as input)

datumaro.util.mask_tools.rles_to_mask(rles, width, height)[source]#
datumaro.util.mask_tools.rle_to_mask(rle_uncompressed: Dict[str, ndarray]) ndarray[source]#

Decode the uncompressed RLE string to the binary mask (2D np.ndarray)

The uncompressed RLE string can be obtained by the datumaro.util.mask_tools.mask_to_rle() function

datumaro.util.mask_tools.find_mask_bbox(mask) Tuple[int, int, int, int][source]#
datumaro.util.mask_tools.merge_masks(masks, start=None)[source]#

Merges masks into one, mask order is responsible for z order. To avoid memory explosion on mask materialization, consider passing a generator.

Inputs: a sequence of index masks or (binary mask, index) pairs

Outputs: an index mask