def make_bg_transparent(im, bg_th=0.0, set_to_color=None):
    if (len(im.shape) == 3):
        alpha_c = (np.sum(im[:, :, :], axis=2) > bg_th).astype(im.dtype)
        (c1, c2, c3) = cv2.split(im)
    else:
        alpha_c = (im[:, :] > bg_th).astype(im.dtype)
        (c1, c2, c3) = (im.copy(), im.copy(), im.copy())
    if (set_to_color is not None):
        zeros = np.zeros_like(c1)
        if (set_to_color == 'green'):
            merged = np.stack([zeros, c2, zeros, alpha_c], axis=(- 1))
        elif (set_to_color == 'red'):
            merged = np.stack([c1, zeros, zeros, alpha_c], axis=(- 1))
        elif (set_to_color == 'royalblue'):
            merged = np.stack([c1, zeros, zeros, alpha_c], axis=(- 1))
        elif (set_to_color == 'violet'):
            merged = np.stack([c1, zeros, c3, alpha_c], axis=(- 1))
        elif (set_to_color == 'yellow'):
            merged = np.stack([c1, c2, zeros, alpha_c], axis=(- 1))
    else:
        merged = np.stack([c1, c2, c3, alpha_c], axis=(- 1))
    return merged
def make_bg_transparent(im, bg_th=0.0, set_to_color=None):
    if (len(im.shape) == 3):
        alpha_c = (np.sum(im[:, :, :], axis=2) > bg_th).astype(im.dtype)
        (c1, c2, c3) = cv2.split(im)
    else:
        alpha_c = (im[:, :] > bg_th).astype(im.dtype)
        (c1, c2, c3) = (im.copy(), im.copy(), im.copy())
    if (set_to_color is not None):
        zeros = np.zeros_like(c1)
        if (set_to_color == 'green'):
            merged = np.stack([zeros, c2, zeros, alpha_c], axis=(- 1))
        elif (set_to_color == 'red'):
            merged = np.stack([c1, zeros, zeros, alpha_c], axis=(- 1))
        elif (set_to_color == 'royalblue'):
            merged = np.stack([c1, zeros, zeros, alpha_c], axis=(- 1))
        elif (set_to_color == 'violet'):
            merged = np.stack([c1, zeros, c3, alpha_c], axis=(- 1))
        elif (set_to_color == 'yellow'):
            merged = np.stack([c1, c2, zeros, alpha_c], axis=(- 1))
    else:
        merged = np.stack([c1, c2, c3, alpha_c], axis=(- 1))
    return merged