def wrapper_compute_color2d_slic_features_labels(img_annot, sp_size, sp_regul, dict_features, label_purity): img, annot = img_annot # in case of binary annotation convert it to integers labels annot = annot.astype(int) assert img.shape[:2] == annot.shape[:2], \ 'image (%s) and annot (%s) should match' \ % (repr(img.shape), repr(annot.shape)) slic, features = compute_color2d_superpixels_features(img, dict_features, sp_size=sp_size, sp_regul=sp_regul, fts_norm=False) neg_label = np.max(annot) + 1 if np.sum(annot < 0) > 0 else None if neg_label is not None: annot[annot < 0] = neg_label label_hist = seg_lbs.histogram_regions_labels_norm(slic, annot) labels = np.argmax(label_hist, axis=1) purity = np.max(label_hist, axis=1) if neg_label is not None: labels[labels == neg_label] = -1 labels[purity < label_purity] = -1 return slic, features, labels
def load_image_annot_compute_features_labels(idx_row, params, show_debug_imgs=SHOW_DEBUG_IMAGES ): """ load image and annotation, and compute superpixel features and labels :param (int, {...}) idx_row: row from table with paths :param {str: ...} params: segmentation parameters :param bool show_debug_imgs: whether show debug images :return (...): """ def _path_out_img(params, dir_name, name): return os.path.join(params['path_exp'], dir_name, name + '.png') idx, row = idx_row idx_name = get_idx_name(idx, row['path_image']) img = load_image(row['path_image'], params['img_type']) annot = load_image(row['path_annot'], '2d_segm') logging.debug('.. processing: %s', idx_name) assert img.shape[:2] == annot.shape[:2], \ 'individual size of image %s and seg_pipe %s for "%s" - "%s"' % \ (repr(img.shape), repr(annot.shape), row['path_image'], row['path_annot']) if show_debug_imgs: plt.imsave(_path_out_img(params, FOLDER_IMAGE, idx_name), img, cmap=plt.cm.gray) plt.imsave(_path_out_img(params, FOLDER_ANNOT, idx_name), annot) # duplicate gray band to be as rgb # if img.ndim == 2: # img = np.rollaxis(np.tile(img, (3, 1, 1)), 0, 3) slic = seg_spx.segment_slic_img2d(img, sp_size=params['slic_size'], rltv_compact=params['slic_regul']) img = tl_data.convert_img_color_from_rgb(img, params.get('clr_space', 'rgb')) logging.debug('computed SLIC with %i labels', slic.max()) if show_debug_imgs: img_rgb = use_rgb_image(img) img_slic = segmentation.mark_boundaries(img_rgb, slic, color=(1, 0, 0), mode='subpixel') plt.imsave(_path_out_img(params, FOLDER_SLIC, idx_name), np.clip(img_slic, 0, 1)) slic_label_hist = seg_label.histogram_regions_labels_norm(slic, annot) labels = np.argmax(slic_label_hist, axis=1) slic_annot = labels[slic] if show_debug_imgs: plt.imsave(_path_out_img(params, FOLDER_SLIC_ANNOT, idx_name), np.clip(slic_annot, 0, slic_annot.max())) features, feature_names = seg_fts.compute_selected_features_img2d( img, slic, params['features']) return idx_name, img, annot, slic, features, labels, \ slic_label_hist, feature_names
def test_count_transitions_segment(self): img = self.img[:, :, 0] annot = self.annot.astype(int) slic = segment_slic_img2d(img, sp_size=15, relative_compact=0.2) label_hist = histogram_regions_labels_norm(slic, annot) labels = np.argmax(label_hist, axis=1) trans = count_label_transitions_connected_segments({'a': slic}, {'a': labels}) path_csv = os.path.join(PATH_OUTPUT, 'labels_transitions.csv') pd.DataFrame(trans).to_csv(path_csv) gc_regul = compute_pairwise_cost_from_transitions(trans, 10.) np.random.seed(0) features = np.tile(labels, (5, 1)).T.astype(float) features += np.random.random(features.shape) - 0.5 gmm = estim_class_model_gmm(features, 4) proba = gmm.predict_proba(features) segment_graph_cut_general(slic, proba, gc_regul)
def wrapper_compute_color2d_slic_features_labels(img_annot, sp_size, sp_regul, dict_features, label_purity): img, annot = img_annot # in case of binary annotation convert it to integers labels annot = annot.astype(int) if img.shape[:2] != annot.shape[:2]: raise ImageDimensionError('image %r and annot %r should match' % (img.shape, annot.shape)) slic, features = compute_color2d_superpixels_features(img, dict_features, sp_size=sp_size, sp_regul=sp_regul) neg_label = np.max(annot) + 1 if np.sum(annot < 0) > 0 else None if neg_label is not None: annot[annot < 0] = neg_label label_hist = histogram_regions_labels_norm(slic, annot) labels = np.argmax(label_hist, axis=1) purity = np.max(label_hist, axis=1) if neg_label is not None: labels[labels == neg_label] = -1 labels[purity < label_purity] = -1 return slic, features, labels