json2mask
- tomni.transformers.json2mask(json_objects: list, img_shape: tuple, minimum_size_contours: int = 3) numpy.ndarray [source]
Convert a list of JSON objects in standard AxionBio format to a binary mask.
- Parameters
json_objects (list) – A list of JSON objects with annotations in the standard AxionBio format.
img_shape (tuple) – The dimensions (height, width) of the mask.
minimum_size_contours (int, optional) – The minimum number of points a contour should have to be included. Defaults to 3. Set to 0 to include all contours regardless of size.
- Returns
A binary mask as a NumPy array.
- Return type
np.ndarray
- Raises
TypeError – If a JSON object of an unsupported type is encountered.
Example:
json_objects = [ { "type": "polygon", "points": [ {"x": 0, "y": 0}, {"x": 0, "y": 5}, {"x": 5, "y": 5}, {"x": 5, "y": 0}, ], } ] img_shape = (10, 10) minimum_size_contours = 3 result = json2mask(json_objects, img_shape, minimum_size_contours) print(result) [[1 1 1 1 1 0 0 0 0 0] [1 1 1 1 1 0 0 0 0 0] [1 1 1 1 1 0 0 0 0 0] [1 1 1 1 1 0 0 0 0 0] [1 1 1 1 1 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0]]