MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / draw_binary_mask

Method draw_binary_mask

utils/visualizer.py:1047–1096  ·  view source on GitHub ↗

Args: binary_mask (ndarray): numpy array of shape (H, W), where H is the image height and W is the image width. Each value in the array is either a 0 or 1 value of uint8 type. color: color of the mask. Refer to `matplotlib.colors` for

(
        self, binary_mask, color=None, *, edge_color=None, text=None, alpha=0.7, area_threshold=10
    )

Source from the content-addressed store, hash-verified

1045 return self.output
1046
1047 def draw_binary_mask(
1048 self, binary_mask, color=None, *, edge_color=None, text=None, alpha=0.7, area_threshold=10
1049 ):
1050 """
1051 Args:
1052 binary_mask (ndarray): numpy array of shape (H, W), where H is the image height and
1053 W is the image width. Each value in the array is either a 0 or 1 value of uint8
1054 type.
1055 color: color of the mask. Refer to `matplotlib.colors` for a full list of
1056 formats that are accepted. If None, will pick a random color.
1057 edge_color: color of the polygon edges. Refer to `matplotlib.colors` for a
1058 full list of formats that are accepted.
1059 text (str): if None, will be drawn on the object
1060 alpha (float): blending efficient. Smaller values lead to more transparent masks.
1061 area_threshold (float): a connected component smaller than this area will not be shown.
1062
1063 Returns:
1064 output (VisImage): image object with mask drawn.
1065 """
1066 if color is None:
1067 color = random_color(rgb=True, maximum=1)
1068 color = mplc.to_rgb(color)
1069
1070 has_valid_segment = False
1071 binary_mask = binary_mask.astype("uint8") # opencv needs uint8
1072 mask = GenericMask(binary_mask, self.output.height, self.output.width)
1073 shape2d = (binary_mask.shape[0], binary_mask.shape[1])
1074
1075 if not mask.has_holes:
1076 # draw polygons for regular masks
1077 for segment in mask.polygons:
1078 area = mask_util.area(mask_util.frPyObjects([segment], shape2d[0], shape2d[1]))
1079 if area < (area_threshold or 0):
1080 continue
1081 has_valid_segment = True
1082 segment = segment.reshape(-1, 2)
1083 self.draw_polygon(segment, color=color, edge_color=edge_color, alpha=alpha)
1084 else:
1085 # TODO: Use Path/PathPatch to draw vector graphics:
1086 # https://stackoverflow.com/questions/8919719/how-to-plot-a-complex-polygon
1087 rgba = np.zeros(shape2d + (4,), dtype="float32")
1088 rgba[:, :, :3] = color
1089 rgba[:, :, 3] = (mask.mask == 1).astype("float32") * alpha
1090 has_valid_segment = True
1091 self.output.ax.imshow(rgba, extent=(0, self.output.width, self.output.height, 0))
1092
1093 if text is not None and has_valid_segment:
1094 lighter_color = self._change_color_brightness(color, brightness_factor=0.7)
1095 self._draw_text_in_mask(binary_mask, text, lighter_color)
1096 return self.output
1097
1098 def draw_soft_mask(self, soft_mask, color=None, *, text=None, alpha=0.5):
1099 """

Callers 4

draw_sem_segMethod · 0.95
draw_panoptic_segMethod · 0.95
process_multi_maskMethod · 0.95
interactive_infer_imageFunction · 0.95

Calls 5

draw_polygonMethod · 0.95
_draw_text_in_maskMethod · 0.95
GenericMaskClass · 0.85
areaMethod · 0.80

Tested by

no test coverage detected