datumaro.util.mask_tools#
Functions
|
|
|
|
|
Find all segments occluded by others and crop them to the visible part only. |
|
Convert an instance mask to polygons |
|
|
|
Generates colors using PASCAL VOC algorithm. |
|
|
|
|
|
|
|
|
|
|
|
Create an index mask from a binary mask by filling a given index value. |
|
Convert an instance mask to bboxes |
|
Convert an instance mask to polygons |
|
|
|
|
|
Merges masks into one, mask order is responsible for z order. |
|
Applies colormap to index mask |
|
Changes mask elements from one colormap to another |
|
Decode the uncompressed RLE string to the binary mask (2D np.ndarray) |
|
|
|
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.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.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)