mask2json

tomni.transformers.mask2json(mask: numpy.ndarray, minimum_size_contours: int = 3, is_diagonal_connected=True, return_inner_contours: bool = False) list[source]

Converts a binary mask into a list of JSON objects representing contours.

Parameters
  • mask (np.ndarray) – A binary mask represented as a NumPy array.

  • minimum_size_contours (int, optional) – The minimum number of points a contour should have to be included. Contours with fewer points will be excluded. Defaults to 3.

  • is_diagonal_connected (bool, optional) – If True, diagonal pixels (e.g., [[1, 0], [0, 1]]) will be considered part of the same object during contour extraction. Defaults to True.

  • return_inner_contours (bool, optional) – If True, include internal contours (holes within the shape). If False, only return the external contours (outlines of the shapes). Defaults to False.

Returns

A list of JSON objects representing contours in the mask.

Return type

list

Example output:

{
    'type': 'polygon',
    'points': [{'x': x1, 'y': y1}, {'x': x2, 'y': y2}, ...],
    'innerObjects': [
        {
            'type': 'polygon',
            'points': [{'x': x3, 'y': y3}, {'x': x4, 'y': y4}, ...],
        },
    ],
}