def draw_instance_contours(img, gti, gtl=None, thickness=2, alpha=1, color=None): """ img = util.imread('/home/joncrall/remote/aretha/data/UrbanMapper3D/training/TAM_Tile_003_RGB.tif') gti = util.imread(ub.truepath('~/remote/aretha/data/UrbanMapper3D/training/TAM_Tile_003_GTI.tif')) gtl = util.imread('/home/joncrall/remote/aretha/data/UrbanMapper3D/training/TAM_Tile_003_GTL.tif') thickness = 2 alpha = 1 """ import cv2 grouped_contours = instance_contours(gti) if gtl is not None: unknown_labels = set(np.unique(gti[gtl == 65])) else: unknown_labels = set() known_labels = set(grouped_contours.keys()) - unknown_labels BGR_GREEN = (0, 255, 0) BGR_BLUE = (255, 0, 0) img = util.ensure_float01(img) base = np.ascontiguousarray((255 * img[:, :, 0:3]).astype(np.uint8)) # Draw an image to overlay first draw_img = np.zeros(base.shape, dtype=np.uint8) if color is None: color = BGR_GREEN known_contours = np.array(list(ub.flatten(list(ub.take(grouped_contours, known_labels))))) draw_img = cv2.drawContours( image=draw_img, contours=known_contours, contourIdx=-1, color=color, thickness=thickness) if unknown_labels: unknown_contours = np.array(list(ub.flatten(ub.take(grouped_contours, unknown_labels)))) draw_img = cv2.drawContours( image=draw_img, contours=unknown_contours, contourIdx=-1, color=BGR_BLUE, thickness=thickness) contour_overlay = util.ensure_alpha_channel(draw_img, alpha=0) contour_overlay.T[3].T[draw_img.sum(axis=2) > 0] = alpha # zero out the edges to avoid visualization errors contour_overlay[0:thickness, :, :] = 0 contour_overlay[-thickness:, :, :] = 0 contour_overlay[:, 0:thickness, :] = 0 contour_overlay[:, -thickness:, :] = 0 # img1 = contour_overlay # img2 = base # from clab import profiler # _ = profiler.profile_onthefly(util.overlay_alpha_images)(contour_overlay, base, keepalpha=False) draw_img = util.overlay_alpha_images(contour_overlay, base, keepalpha=False) draw_img = np.ascontiguousarray((255 * draw_img[:, :, 0:3]).astype(np.uint8)) return draw_img
def draw_mask_contours(img, gt, thickness=2, alpha=1): import cv2 gt = gt.astype(np.uint8) border = cv2.copyMakeBorder(gt, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=0 ) _, contours, hierarchy = cv2.findContours(border, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE, offset=(-1, -1)) BGR_GREEN = (0, 255, 0) img = util.ensure_float01(img) base = np.ascontiguousarray((255 * img[:, :, 0:3]).astype(np.uint8)) if alpha >= 1: draw_img = cv2.drawContours( image=base, contours=contours, contourIdx=-1, color=BGR_GREEN, thickness=thickness) else: # Draw an image to overlay first draw_img = cv2.drawContours( image=np.zeros(base.shape, dtype=np.uint8), contours=contours, contourIdx=-1, color=BGR_GREEN, thickness=thickness) contour_overlay = util.ensure_alpha_channel(draw_img, alpha=0) contour_overlay.T[3].T[draw_img.sum(axis=2) > 0] = alpha # zero out the edges to avoid visualization errors contour_overlay[0:thickness, :, :] = 0 contour_overlay[-thickness:, :, :] = 0 contour_overlay[:, 0:thickness, :] = 0 contour_overlay[:, -thickness:, :] = 0 draw_img = util.overlay_alpha_images(contour_overlay, base) draw_img = np.ascontiguousarray((255 * draw_img[:, :, 0:3]).astype(np.uint8)) return draw_img
def draw_contours(bgr, contours, thickness=2, alpha=1, color=None): # TODO: move to somewhere better BGR_GREEN = (0, 255, 0) if color is None: color = BGR_GREEN bgr = util.ensure_float01(bgr) base = np.ascontiguousarray((255 * bgr[:, :, 0:3]).astype(np.uint8)) draw_img = np.zeros_like(base) draw_img = cv2.drawContours( image=draw_img, contours=contours, contourIdx=-1, color=color, thickness=thickness) contour_overlay = util.ensure_alpha_channel(draw_img, alpha=0) contour_overlay.T[3].T[draw_img.sum(axis=2) > 0] = alpha draw_img = util.overlay_alpha_images(contour_overlay, base, keepalpha=False) draw_img = np.ascontiguousarray((255 * draw_img[:, :, 0:3]).astype(np.uint8)) return draw_img