def save_mask_triple_isolated_mask(rgb_img, label_img, id_to_class_name_map, class_name, save_fpath) -> None: """ Args: - rgb_img: - label_img: - id_to_class_name_map: - class_name: - save_fpath: Returns: - None """ for id, proposed_class_name in id_to_class_name_map.items(): if class_name == proposed_class_name: break isolated_rgb_mask = np.ones_like(rgb_img) * 255 y, x = np.where(label_img == id) isolated_rgb_mask[y, x, :] = rgb_img.copy()[y, x, :] rgb_with_mask = convert_instance_img_to_mask_img(label_img, rgb_img.copy()) mask_img = convert_instance_img_to_mask_img(label_img) concat_img = form_hstacked_imgs( [rgb_img, isolated_rgb_mask, rgb_with_mask, mask_img], save_fpath, save_to_disk=False) cv2.imwrite(save_fpath, concat_img[:, :, ::-1])
def save_binary_mask_triple(rgb_img: np.ndarray, label_img: np.ndarray, save_fpath: str, save_to_disk: bool = False) -> np.ndarray: """ Currently mask img background is light-blue. Instead, could set it to white. np.array([255,255,255]) Args: - rgb_img: - label_img: - save_fpath - save_to_disk Returns: - Array, representing 3 horizontally concatenated images: from left-to-right, they are RGB, RGB+Semantic Masks, Semantic Masks """ img_h, img_w, _ = rgb_img.shape rgb_with_mask = highlight_binary_mask(label_img, rgb_img.copy()) blank_img = np.ones((img_h, img_w, 3), dtype=np.uint8) * 255 y, x = np.where(label_img == 0) blank_img[y, x, :] = LIME_GREEN # LIGHT_BLUE mask_img = highlight_binary_mask(label_img, blank_img) return form_hstacked_imgs([rgb_img, rgb_with_mask, mask_img], save_fpath, save_to_disk)
def test_form_hstacked_imgs_three(): """ Horizontally stack three 2x2 RGB images into a single 2x6 RGB image. """ hstack_save_fpath = 'htmp1.png' img1 = np.zeros((2,2,3), dtype=np.uint8) img1[0,0,:] = [255,0,1] img2 = np.zeros((2,2,3), dtype=np.uint8) img2[1,1,:] = [5,10,15] img3 = np.zeros((2,2,3), dtype=np.uint8) img3[0,1,:] = [255,254,253] img_list = [img1, img2, img3] hstack_img = form_hstacked_imgs(img_list, hstack_save_fpath, save_to_disk=False) # now has 6 columns gt_hstack_img = np.zeros((2,6,3), dtype=np.uint8) gt_hstack_img[0,0,:] = [255,0,1] gt_hstack_img[1,3,:] = [5,10,15] gt_hstack_img[0,5,:] = [255,254,253] assert np.allclose(hstack_img, gt_hstack_img)
def form_mask_triple_embedded_classnames( rgb_img: np.ndarray, label_img: np.ndarray, id_to_class_name_map: Mapping[int, str], save_fpath: str, save_to_disk: bool = False) -> np.ndarray: """ Args: - rgb_img: - label_img: - id_to_class_name_map - save_fpath - save_to_disk Returns: - Array, representing 3 horizontally concatenated images: from left-to-right, they are RGB, RGB+Semantic Masks, Semantic Masks """ rgb_with_mask = convert_instance_img_to_mask_img(label_img, rgb_img.copy()) # or can do max cardinality conn comp of each class rgb2 = save_classnames_in_image_sufficientpx(rgb_with_mask, label_img, id_to_class_name_map) mask_img = convert_instance_img_to_mask_img(label_img, img_rgb=None) rgb3 = save_classnames_in_image_sufficientpx(mask_img, label_img, id_to_class_name_map) return form_hstacked_imgs([rgb_img, rgb2, rgb3], save_fpath, save_to_disk)
def save_img_with_blendedmaskimg( rgb_img: np.ndarray, label_img: np.ndarray, save_fpath: str, save_to_disk: bool = False) -> None: """ Args: - rgb_img: - label_img: - save_fpath - save_to_disk Returns: - Array, representing 3 horizontally concatenated images: from left-to-right, they are RGB, RGB+Semantic Masks, Semantic Masks """ rgb_with_mask = highlight_binary_mask(label_img, rgb_img.copy()) return form_hstacked_imgs([rgb_img, rgb_with_mask], save_fpath, save_to_disk)
def form_mask_triple(rgb_img: np.ndarray, label_img: np.ndarray, save_fpath: str, save_to_disk: bool = False) -> None: """ Args: - rgb_img: - label_img: - save_fpath - save_to_disk Returns: - Array, representing 3 horizontally concatenated images: from left-to-right, they are RGB, RGB+Semantic Masks, Semantic Masks """ rgb_with_mask = convert_instance_img_to_mask_img(label_img, rgb_img.copy()) mask_img = convert_instance_img_to_mask_img(label_img, img_rgb=None) return form_hstacked_imgs([rgb_img, rgb_with_mask, mask_img], save_fpath, save_to_disk)
def save_binary_mask_double( rgb_img: np.ndarray, label_img: np.ndarray, save_fpath: str, save_to_disk: bool = False) -> None: """ Currently blended mask img background is lime green. Args: - rgb_img: - label_img: - save_fpath - save_to_disk Returns: - Array, representing 2 horizontally concatenated images: from left-to-right, they are RGB, RGB+Semantic Masks """ img_h, img_w, _ = rgb_img.shape lime_green_rgb = vis_mask(rgb_img.copy(), 1 - label_img, LIME_GREEN, alpha=0.2) rgb_with_mask = highlight_binary_mask(label_img, lime_green_rgb) return form_hstacked_imgs([rgb_img, rgb_with_mask], save_fpath, save_to_disk)
def test_form_hstacked_imgs_two(): """ Horizontally stack two 2x2 RGB images into a single 2x4 RGB image. """ hstack_save_fpath = 'htmp2.png' img1 = np.zeros((2,2,3), dtype=np.uint8) img1[0,0,:] = [255,0,1] img2 = np.zeros((2,2,3), dtype=np.uint8) img2[1,1,:] = [5,10,15] img_list = [img1, img2] hstack_img = form_hstacked_imgs(img_list, hstack_save_fpath, save_to_disk=False) gt_hstack_img = np.zeros((2,4,3), dtype=np.uint8) gt_hstack_img[0,0,:] = [255,0,1] gt_hstack_img[1,3,:] = [5,10,15] assert np.allclose(hstack_img, gt_hstack_img)
def save_mask_triple_isolated_mask(rgb_img: np.ndarray, label_img: np.ndarray, id_to_class_name_map, class_name: str, save_fpath: str) -> None: """ Save a triplet of images to disk (RGB image, label map, and a blended version of the two). Args: rgb_img: label_img: id_to_class_name_map: class_name: save_fpath: """ for id, proposed_class_name in id_to_class_name_map.items(): if class_name == proposed_class_name: break isolated_rgb_mask = np.ones_like(rgb_img) * 255 y,x = np.where(label_img == id) isolated_rgb_mask[y,x,:] = rgb_img.copy()[y,x,:] rgb_with_mask = convert_instance_img_to_mask_img(label_img, rgb_img.copy()) mask_img = convert_instance_img_to_mask_img(label_img) concat_img = form_hstacked_imgs([rgb_img, isolated_rgb_mask, rgb_with_mask, mask_img], save_fpath, save_to_disk=False) cv2.imwrite(save_fpath, concat_img[:,:,::-1])