def test_segm_supervised(self): img = resize(self.img, output_shape=(256, 256)) annot = resize(self.annot, output_shape=(256, 256), order=0, preserve_range=True).astype(int) img2 = resize(self.img2, output_shape=(256, 256)) path_dir = os.path.join(PATH_OUTPUT, 'temp_segm-supervised_gc') if not os.path.exists(path_dir): os.mkdir(path_dir) sp_size = 10 tp_edge = ['model', 'const'] list_regul = [0, 1, 10] dict_imgs = dict() name = 'fig-img%i_regul-%.2f_edge-%s.png' classif, _, _, _ = train_classif_color2d_slic_features( [img], [annot], sp_size=sp_size, dict_features=FEATURES_TEXTURE) segment_color2d_slic_features_model_graphcut( img, classif, sp_size=sp_size, gc_regul=0., dict_features=FEATURES_TEXTURE, debug_visual=dict_imgs) show_segm_debugs_2d(dict_imgs, path_dir, name % (1, 0, '_debug')) for edge in tp_edge: dict_imgs = dict() for regul in list_regul: seg, _ = segment_color2d_slic_features_model_graphcut( img, classif, dict_features=FEATURES_TEXTURE, sp_size=sp_size, gc_regul=regul, gc_edge_type=edge) show_segm_results_2d(img, seg, path_dir, name % (1, regul, edge)) seg, _ = segment_color2d_slic_features_model_graphcut( img2, classif, dict_features=FEATURES_TEXTURE, sp_size=sp_size, gc_regul=regul, gc_edge_type=edge, debug_visual=dict_imgs) show_segm_results_2d(img2, seg, path_dir, name % (2, regul, edge)) show_segm_debugs_2d(dict_imgs, path_dir, name % (2, regul, edge)) dict_imgs = None
def run_segm2d_gmm_gc(img2d, dir_name, params, types_edge=('model', 'const'), list_regul=(0, 0.5, 1, 3, 5, 10)): """ perform several experiment with different edge type and GC regul. :param ndarray img2d: input image :param str dir_name: create the folder in output path :param list(str) types_edge: list of performed edge types :param dict params: segmentation parameters :param list(float) list_regul: list of performed edge types """ path_dir = os.path.join(PATH_OUTPUT, dir_name) if not os.path.isdir(path_dir): os.mkdir(path_dir) model, _ = estim_model_classes_group([img2d], model_type='GMM', **params) params.pop('nb_classes', None) params.pop('pca_coef', None) for edge in types_edge: dict_imgs = {} for regul in list_regul: seg, _ = segment_color2d_slic_features_model_graphcut( img2d, model, gc_regul=regul, gc_edge_type=edge, debug_visual=dict_imgs, **params ) show_segm_debugs_2d(dict_imgs, path_dir, 'fig_regul-%.2f_edge-%s_debug.png' % (regul, edge)) show_segm_results_2d(img2d, seg, path_dir, 'fig_regul-%.2f_edge-%s.png' % (regul, edge)) dict_imgs = None
def process_image( img_path: str = 'data-images/drosophila_disc/image/img_5.jpg', nb_classes: int = 2, spx_size: int = 30, spx_regul: float = 0.5, gc_regul: float = 0.4, streamlit_app: bool = False, ): if not img_path: return st.write('loading image...') img = plt.imread(img_path) # if streamlit_app: # st.image(img) debug = {} spx_config = dict(sp_size=spx_size, sp_regul=spx_regul, dict_features=FEATURES_SET_MIN) st.write('estimating model...') model, _ = estim_model_classes_group([img], nb_classes=nb_classes, **spx_config) st.write('performing GC segmentation...') segm, _ = segment_color2d_slic_features_model_graphcut(img, model, **spx_config, gc_regul=gc_regul, debug_visual=debug) print(debug.keys()) spx_contour = ski_segm.mark_boundaries(debug['image'], debug['slic'], color=(1, 0, 0), mode='subpixel') st.write('preparing visualization...') fig, axarr = plt.subplots(ncols=3, nrows=2, figsize=(18, 12), tight_layout=True) axarr[0, 0].set_title("Original image") axarr[0, 0].imshow(debug['image']) axarr[0, 1].set_title("Superpixel contours") axarr[0, 1].imshow(spx_contour) axarr[0, 2].set_title("Image represented by superpixel colour means") axarr[0, 2].imshow(debug['slic_mean'] / 255.) axarr[1, 0].set_title("Graph edges with colour importance") axarr[1, 0].imshow(debug['img_graph_edges']) axarr[1, 1].set_title("Segmentation graph") axarr[1, 1].imshow(debug['img_graph_segm']) axarr[1, 2].set_title("Output segmentation") axarr[1, 2].imshow(segm) if streamlit_app: st.pyplot(fig)
def segment_image_model(imgs_idx_path, params, model, path_out=None, path_visu=None, show_debug_imgs=SHOW_DEBUG_IMAGES): """ segment image with already estimated model :param (int, str) imgs_idx_path: :param {str: ...} params: segmentation parameters :param obj scaler: :param obj pca: :param obj model: :param str path_out: path to dir with segmentation :param str path_visu: path to dir with debug images :param bool show_debug_imgs: whether show debug images :return (str, 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']) 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.segment_color2d_slic_features_model_graphcut( img, model, sp_size=params['slic_size'], sp_regul=params['slic_regul'], dict_features=params['features'], 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(np.sqrt(np.prod(segm.shape)) * 0.01) 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 segment_image_model(imgs_idx_path, params, scaler, pca, model, path_out=None, path_visu=None): """ segment image with already estimated model :param (int, str) imgs_idx_path: :param {str: ...} params: segmentation parameters :param obj scaler: :param obj pca: :param obj model: :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(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']) path_img = os.path.join(params['path_exp'], FOLDER_IMAGE, idx_name + '.png') tl_data.io_imsave(path_img, img.astype(np.uint8)) dict_debug_imgs = dict() if SHOW_DEBUG_IMAGES else None try: segm = seg_pipe.segment_color2d_slic_features_model_graphcut( img, scaler, pca, model, clr_space=params['clr_space'], sp_size=params['slic_size'], sp_regul=params['slic_regul'], dict_features=params['features'], gc_regul=params['gc_regul'], gc_edge_type=params['gc_edge_type'], dict_debug_imgs=dict_debug_imgs) except Exception: logging.error(traceback.format_exc()) segm = np.zeros(img.shape[:2]) export_visual(idx_name, img, segm, dict_debug_imgs, path_out, path_visu) # gc.collect(), time.sleep(1) return idx_name, segm
def run_segm2d_gmm_gc(img2d, dir_name, types_edge=('model', 'const'), list_regul=(0, 0.5, 1, 3, 5, 10), dict_params=None): """ perform several experiment with different edge type and GC regul. :param ndarray img2d: input image :param str dir_name: create the folder in output path :param [str] types_edge: list of performed edge types :param [float] list_regul: list of performed edge types :param {str: ...} dict_params: segmentation parameters :return: """ path_dir = os.path.join(PATH_OUTPUT, dir_name) if dict_params is None: dict_params = dict() if not os.path.isdir(path_dir): os.mkdir(path_dir) scaler, pca, model = pipelines.estim_model_classes_group([img2d], proba_type='GMM', **dict_params) dict_params.pop('nb_classes', None) dict_params.pop('pca_coef', None) for edge in types_edge: dict_imgs = dict() for regul in list_regul: # seg = pipelines.pipe_color2d_slic_features_gmm_graphcut( # img2d, gc_regul=regul, gc_edge_type=edge, # dict_debug_imgs=dict_imgs, **dict_params) seg = pipelines.segment_color2d_slic_features_model_graphcut( img2d, scaler, pca, model, gc_regul=regul, gc_edge_type=edge, dict_debug_imgs=dict_imgs, **dict_params) show_segm_debugs_2d( dict_imgs, path_dir, 'fig_regul-%.2f_edge-%s_debug.png' % (regul, edge)) show_segm_results_2d(img2d, seg, path_dir, 'fig_regul-%.2f_edge-%s.png' % (regul, edge)) dict_imgs = None
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
segments = [] #Amostra supervised segmentation on images i = 0 for i in range(len(tensores)): img = tensores[i] #_= plt.imshow(img) FIG_SIZE = (8. * numpy.array(img.shape[:2]) / numpy.max(img.shape))[::-1] nb_classes = 4 sp_size = 1 sp_regul = 0.1 dict_features = {'color': ['mean', 'std', 'median']} #modelo baseado na segmentação da imagme em classes model, _ = segm_pipe.estim_model_classes_group([img], nb_classes, sp_size=sp_size, sp_regul=sp_regul, dict_features=dict_features, pca_coef=True, model_type='GMM') dict_debug = {} seg, _ = segm_pipe.segment_color2d_slic_features_model_graphcut(img, model, sp_size=sp_size, sp_regul=sp_regul, dict_features=dict_features, gc_regul=1., gc_edge_type='color', debug_visual=dict_debug) segments.append(seg) fig = plt.figure(figsize=FIG_SIZE) print('Exemplo n°: ', str(i)) plt.imshow(img) plt.imshow(seg, cmap=plt.cm.jet) _= plt.contour(seg, levels=numpy.unique(seg), colors='w') print('Classe: ', 'Aprovado' if y[i] == 1 else 'Reprovado', '\n', numpy.unique(seg, return_counts=True)) print(seg) lista_classes = [] i = 0 for i in range(len(segments)): lista_classes.append(numpy.unique(segments[i], return_counts=True)[1]) i = 0 plt.rcParams["figure.figsize"] = (14, 6)