overlay_prediction_contours¶

overlay_prediction_contours(canvas, inst_dict, type_colours=None, inst_colours=(255, 255, 0), line_thickness=2, *, draw_dot)[source]¶

Overlaying instance contours on image.

Internally, colours from type_colours are prioritized over inst_colours. However, if inst_colours is None and type_colours is not provided, random colour is generated for each instance.

Parameters:
  • canvas (numpy.ndarray) – Image to draw predictions on.

  • inst_dict (dict) – Dictionary of instances. It is expected to be in the following format: {instance_id: {type: int, contour: List[List[int]], centroid:List[float]}.

  • draw_dot (bool) – To draw a dot for each centroid or not.

  • type_colours (dict) – A dict of {type_id : (type_name, colour)}, type_id is from 0 to N and colour is a tuple of (r, g, b).

  • inst_colours (tuple, np.ndarray) – A colour to assign for all instances, or a list of colours to assigned for each instance in inst_dict. By default, all instances will have RGB colour (255, 255, 0).

  • line_thickness (int) – Line thickness of contours.

Returns:

The overlaid image.

Return type:

numpy.ndarray

Examples

>>> from tiatoolbox.utils.visualization import overlay_prediction_contours
>>> import numpy as np
>>> from matplotlib import pyplot as plt
>>> # Generate a random example; replace with your own data
>>> canvas = np.zeros((256, 256, 3), dtype=np.uint8)
>>> inst_dict = {
...     1: {
...         "type": 0,
...         "contour": [[50, 50], [60, 45], [70, 50],
...                     [70, 60], [60, 65], [50, 60]],
...         "centroid": [60, 55]
...         },
...     2: {
...         "type": 1,
...         "contour": [[100, 100], [120, 100], [120, 120], [100, 120]],
...         "centroid": [110, 110]
...         }
... }
>>> type_colours = {
...     0: ("Type A", (0, 255, 0)),
...     1: ("Type B", (0, 0, 255))
... }
>>> # Example usage of overlay_prediction_contours
>>> overlaid_canvas = overlay_prediction_contours(
...     canvas=canvas,
...     inst_dict=inst_dict,
...     type_colours=type_colours,
...     line_thickness=1,
...     draw_dot=True
... )
>>> plt.imshow(overlaid_canvas)
>>> plt.show()