def export_visual(idx_name, img, segm, debug_visual=None, path_out=None, path_visu=None): """ export visualisations :param str idx_name: :param ndarray img: input image :param ndarray segm: resulting segmentation :param debug_visual: dictionary with debug images :param str path_out: path to dir with segmentation :param str path_visu: path to dir with debug images """ logging.info('export results and visualization...') if set(np.unique(segm)) <= {0, 1}: segm *= 255 path_img = os.path.join(path_out, str(idx_name) + '.png') logging.debug('exporting segmentation: %s', path_img) im_seg = Image.fromarray(segm.astype(np.uint8)) im_seg.convert('L').save(path_img) # io.imsave(path_img, segm) if path_visu is not None and os.path.isdir(path_visu): path_fig = os.path.join(path_visu, str(idx_name) + '.png') logging.debug('exporting segmentation results: %s', path_fig) fig = tl_visu.figure_image_segm_results(img, segm) fig.savefig(path_fig) plt.close(fig) if path_visu is not None and os.path.isdir(path_visu) and debug_visual is not None: path_fig = os.path.join(path_visu, str(idx_name) + '_debug.png') logging.debug('exporting (debug) visualization: %s', path_fig) fig = tl_visu.figure_segm_graphcut_debug(debug_visual) fig.savefig(path_fig, bbox_inches='tight', pad_inches=0.1) plt.close(fig)
def show_segm_debugs_2d(images, path_dir, fig_name='temp-debug_.png'): """ show and expert partial segmentation results :param dict images: :param str path_dir: path to the visualisations :param str fig_name: figure name """ if not images: return fig = figure_segm_graphcut_debug(images) path_fig = os.path.join(path_dir, fig_name) fig.savefig(path_fig, bbox_inches='tight', pad_inches=0.1) if logging.getLogger().getEffectiveLevel() == logging.DEBUG: plt.show() plt.close(fig)
def segment_image(imgs_idx_path, params, classif, path_out, path_visu=None, show_debug_imgs=SHOW_DEBUG_IMAGES): """ perform image segmentation on input image with given paramters and trained classifier, and save results :param (int, str) imgs_idx_path: :param {str: ...} params: segmentation parameters :param obj classif: trained classifier :param str path_out: path for output :param str path_visu: the existing patch means export also visualisation :param bool show_debug_imgs: whether show debug images :return (str, ndarray, ndarray): """ idx, path_img = parse_imgs_idx_path(imgs_idx_path) logging.debug('segmenting image: "%s"', path_img) idx_name = get_idx_name(idx, path_img) img = load_image(path_img, params['img_type']) debug_visual = dict() if show_debug_imgs else None gc_regul = params['gc_regul'] if params['gc_use_trans']: label_penalty = seg_gc.compute_pairwise_cost_from_transitions( params['label_transitions']) gc_regul = (gc_regul * label_penalty) segm_gc, segm_soft = seg_pipe.segment_color2d_slic_features_model_graphcut( img, classif, sp_size=params['slic_size'], sp_regul=params['slic_regul'], dict_features=params['features'], gc_regul=gc_regul, gc_edge_type=params['gc_edge_type'], debug_visual=debug_visual) segm_map = np.argmax(segm_soft, axis=-1) for segm, suffix in [(segm_gc, ''), (segm_map, '_MAP')]: path_img = os.path.join(path_out, idx_name + suffix + '.png') logging.debug('export segmentation: %s', path_img) if np.max(segm) <= 1: img_seg = Image.fromarray((segm * 255).astype(np.uint8)) else: img_seg = Image.fromarray(segm.astype(np.uint8)) img_seg.convert('L').save(path_img) # io.imsave(path_img, segm_gc) path_npz = os.path.join(path_out, idx_name + '.npz') np.savez_compressed(path_npz, segm_soft) # plt.imsave(os.path.join(path_out, idx_name + '_rgb.png'), seg_pipe) if params.get('visual', False) and path_visu is not None \ and os.path.isdir(path_visu): export_draw_image_segm_contour(img, segm_gc, path_visu, idx_name, '_GC') export_draw_image_segm_contour(img, segm_map, path_visu, idx_name, '_MAP') if show_debug_imgs and debug_visual is not None: path_fig = os.path.join(path_visu, str(idx_name) + '_debug.png') logging.debug('exporting (debug) visualization: %s', path_fig) fig = tl_visu.figure_segm_graphcut_debug(debug_visual) fig.savefig(path_fig, bbox_inches='tight', pad_inches=0.1) plt.close(fig) gc.collect() time.sleep(1) return idx_name, segm_map, segm_gc