def test_add_text_cv2(): """ Smokescreen """ img = 255 * np.ones((512,512,3), np.uint8) text = 'Hello World!' add_text_cv2(img, text) import matplotlib.pyplot as plt plt.imshow(img) #plt.show() plt.close()
def save_classnames_in_image_sufficientpx( rgb_img: np.ndarray, label_img: np.ndarray, id_to_class_name_map: Mapping[int,str], font_color = (0,0,0), save_to_disk: bool = False, save_fpath: str = '', min_conncomp_px: int = 4000, font_scale: int = 1 ): """ Write a classname over each connected component of a label map as long as the connected component has a sufficiently large number of pixels (specified as argument). Args: - rgb_img: Numpy array (H,W,3) representing RGB image - label_img: Numpy array (H,W) representing label map - id_to_class_name_map: mapping from class ID to classname - font_color: 3-tuple representing RGB font color - save_to_disk: whether to save image to disk - save_fpath: absolute file path - min_conncomp_px: minimum number of pixels to justify placing a text label over connected component - font_scale: scale of font text Returns: - rgb_img: Numpy array (H,W,3) with embedded classanmes """ H, W, C = rgb_img.shape class_to_conncomps_dict = scipy_conn_comp(label_img) for class_idx, conncomps_list in class_to_conncomps_dict.items(): for conncomp in conncomps_list: if conncomp.sum() < min_conncomp_px: continue text = id_to_class_name_map[class_idx] y,x = get_mean_mask_location(conncomp) x -= 55 # move the text so approx. centered over mask. x = max(0,x) x = min(W-1,x) # jitter location if nonconvex object mean not within its mask if conncomp[y,x] != 1: x,y = search_jittered_location_in_mask(x,y,conncomp) # print(f'Class idx: {class_idx}: (x,y)=({x},{y})') rgb_img = add_text_cv2( rgb_img, text, coords_to_plot_at=(x,y), font_color=font_color, font_scale=font_scale, thickness=2) if save_to_disk: cv2_write_rgb(save_fpath, rgb_img) return rgb_img
def form_contained_classes_color_guide( label_img: np.ndarray, id_to_class_name_map: Mapping[int,str], fname_stem: str, save_dir: str, save_to_disk: bool = True, max_colors_per_col: int = 10 ) -> np.ndarray: """ Write out an image explaining the classes inside an image. Args: label_img id_to_class_name_map fname_stem save_dir Returns: palette_img: Array with cells colored with class color from palette """ ids_present = np.unique(label_img) num_cols = math.ceil(len(ids_present) / max_colors_per_col) num_rows = max_colors_per_col palette_img = np.zeros( (NUM_PX_PER_ROW*num_rows,NUM_PX_PER_COL * num_cols,3), dtype=np.uint8 ) for i, labelid in enumerate(ids_present): col_idx = i // max_colors_per_col row_idx = i % max_colors_per_col class_name = id_to_class_name_map[labelid] id_img = np.ones((NUM_PX_PER_ROW,NUM_PX_PER_COL), dtype=np.uint16) * labelid blank_img = 255 * np.ones((NUM_PX_PER_ROW,NUM_PX_PER_COL,3), dtype=np.uint8) blank_img = add_text_cv2(blank_img, str(class_name)) color_img = convert_instance_img_to_mask_img(id_img, blank_img) # vertical pixel start v_start = row_idx * NUM_PX_PER_ROW v_end = (row_idx+1) * NUM_PX_PER_ROW # horizontal pixel start h_start = NUM_PX_PER_COL * col_idx h_end = h_start + NUM_PX_PER_COL palette_img[v_start:v_end,h_start:h_end,:] = color_img palette_save_fpath = f'{save_dir}/{fname_stem}_colors.png' if save_to_disk: cv2.imwrite(palette_save_fpath, palette_img[:,:,::-1]) return palette_img
def save_classnames_in_image_sufficientpx(rgb_img, label_img, id_to_class_name_map, font_color=(0, 0, 0), save_to_disk: bool = False, save_fpath: str = '', min_conncomp_px: int = 4000): """ Args: - rgb_img - label_img - id_to_class_name_map: Mapping[int,str] Returns: - """ H, W, C = rgb_img.shape class_to_conncomps_dict = scipy_conn_comp(label_img) for class_idx, conncomps_list in class_to_conncomps_dict.items(): for conncomp in conncomps_list: if conncomp.sum() < min_conncomp_px: continue text = id_to_class_name_map[class_idx] y, x = get_mean_mask_location(conncomp) x -= 55 # move the text so approx. centered over mask. x = max(0, x) x = min(W - 1, x) # jitter location if nonconvex object mean not within its mask if conncomp[y, x] != 1: x, y = search_jittered_location_in_mask(x, y, conncomp) # print(f'Class idx: {class_idx}: (x,y)=({x},{y})') rgb_img = add_text_cv2(rgb_img, text, coords_to_plot_at=(x, y), font_color=font_color, font_scale=1, thickness=2) if save_to_disk: cv2_write_rgb(save_fpath, rgb_img) return rgb_img
def save_classnames_in_image_maxcardinality( rgb_img, label_img, id_to_class_name_map, font_color = (0,0,0), save_to_disk: bool = False, save_fpath: str = '' ) -> np.ndarray: """ Args: rgb_img label_img id_to_class_name_map: Mapping[int,str] Returns: rgb_img """ H, W, C = rgb_img.shape class_to_conncomps_dict = scipy_conn_comp(label_img) for class_idx, conncomps_list in class_to_conncomps_dict.items(): mask_idx = find_max_cardinality_mask(conncomps_list) maxsz_conncomp = conncomps_list[mask_idx] text = id_to_class_name_map[class_idx] y,x = get_mean_mask_location(maxsz_conncomp) x -= 55 x = max(0,x) x = min(W-1,x) # print(f'Class idx: {class_idx}: (x,y)=({x},{y})') rgb_img = add_text_cv2( rgb_img, text, coords_to_plot_at=(x,y), font_color=font_color, font_scale = 1, thickness=2) if save_to_disk: cv2_write_rgb(save_fpath, rgb_img) return rgb_img