def segment_image_independent(img_idx_path, params, path_out, path_visu=None, show_debug_imgs=SHOW_DEBUG_IMAGES): """ segment image indecently (estimate model just for this model) :param (int, str) img_idx_path: :param {str: ...} params: segmentation parameters :param str path_out: path to dir with segmentation :param str path_visu: path to dir with debug images :return (str, ndarray): """ idx, path_img = parse_imgs_idx_path(img_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']) path_img = os.path.join(params['path_exp'], FOLDER_IMAGE, idx_name + '.png') tl_data.io_imsave(path_img, img.astype(np.uint8)) debug_visual = dict() if show_debug_imgs else None try: segm, segm_soft = seg_pipe.pipe_color2d_slic_features_model_graphcut( img, nb_classes=params['nb_classes'], sp_size=params['slic_size'], sp_regul=params['slic_regul'], dict_features=params['features'], estim_model=params['estim_model'], pca_coef=params['pca_coef'], gc_regul=params['gc_regul'], gc_edge_type=params['gc_edge_type'], debug_visual=debug_visual) path_npz = os.path.join(path_out, idx_name + '.npz') np.savez_compressed(path_npz, segm_soft) except Exception: logging.error(traceback.format_exc()) segm = np.zeros(img.shape[:2]) boundary_size = int(params['slic_size'] * 3) segm = seg_lbs.assume_bg_on_boundary(segm, bg_label=0, boundary_size=boundary_size) export_visual(idx_name, img, segm, debug_visual, path_out, path_visu) # gc.collect(), time.sleep(1) return idx_name, segm
def test_segm_gmm_gc_objects(self): img = imresize(self.img_obj, (256, 256)) logging.debug('dimension: {}'.format(img.shape)) dict_imgs = dict() path_dir = os.path.join(PATH_OUTPUT, 'temp_segm-gmm-gc-objects') if not os.path.isdir(path_dir): os.mkdir(path_dir) seg, _ = pipe_color2d_slic_features_model_graphcut( img, nb_classes=4, dict_features={'color': ['mean']}, sp_size=20, sp_regul=0.2, gc_regul=1., gc_edge_type='model', debug_visual=dict_imgs) show_segm_debugs_2d(dict_imgs, path_dir, 'fig_regul-%.2f_edge-%s_debug.png' % (1., 'model')) show_segm_results_2d(img, seg, path_dir, 'fig_regul-%.2f_edge-%s.png' % (1., 'model'))