def hierarchical_merging_of_region_boundary_rags_example(): #img = data.coffee() edges = sobel(skimage.color.rgb2gray(img)) labels = slic(img, compactness=30, n_segments=400) g = graph.rag_boundary(labels, edges) graph.show_rag(labels, g, img) plt.title('Initial RAG') labels2 = graph.merge_hierarchical(labels, g, thresh=0.08, rag_copy=False, in_place_merge=True, merge_func=merge_boundary, weight_func=weight_boundary) graph.show_rag(labels, g, img) plt.title('RAG after hierarchical merging') plt.figure() out = skimage.color.label2rgb(labels2, img, kind='avg') plt.imshow(out) plt.title('Final segmentation') plt.show()
def show_segmen_rag(array, pathlibpath, numSegments, threshold, cedges=177, compactness=0.1, sigma=5, convert2lab=False): print('Obtaining superstructures') segments = slic(array, compactness=compactness, n_segments=numSegments, sigma=sigma, multichannel=False, convert2lab=convert2lab) print('Number of SV: ', len(np.unique(segments))) segments += 1 edges = filters.sobel(array) print('Obtaining RAG') rag = graph.rag_boundary(segments, edges, connectivity=1) segments2 = graph.merge_hierarchical(segments, rag, threshold=threshold, in_place_merge=True, rag_copy=False, merge_func=merge_boundary, weight_func=weight_boundary) print('Final Number of SV: ', len(np.unique(segments2))) graph.show_rag(segments, rag, array, edge_cmap='viridis') save_yorno(array, segments, pathlibpath, cedges)
def plotRagwithColorMaps(self, img, labels): g = graph.rag_mean_color(img, labels) fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True, figsize=(6, 8)) ax[0].set_title('RAG drawn with default settings') lc = graph.show_rag(labels, g, img, ax=ax[0]) # specify the fraction of the plot area that will be used to draw the colorbar fig.colorbar(lc, fraction=0.03, ax=ax[0]) ax[1].set_title('RAG drawn with grayscale image and viridis colormap') lc = graph.show_rag(labels, g, img, img_cmap='gray', edge_cmap='viridis', ax=ax[1]) fig.colorbar(lc, fraction=0.03, ax=ax[1]) for a in ax: a.axis('off') plt.tight_layout() plt.show()
def generate_texture(img, slic_n=200, slic_compactness=25, rag_from_binary=False, edge_threshold=1e-4, verbose=False): img = img_as_float(img) if rag_from_binary: if verbose: print('Applying initial thresholding...', flush=True) grey_img = color.rgb2grey(img) binary_mask = (grey_img > threshold_li(grey_img)) * 1.0 if verbose: print('Constructing superpixels...', flush=True) segments = slic(img, n_segments=slic_n, compactness=slic_compactness) if verbose: out_avg = color.label2rgb(segments, img, kind='avg') io.imshow(out_avg) io.show() if verbose: print('Building RAG...', flush=True) if rag_from_binary: RAG = graph.rag_mean_color(binary_mask, segments, mode='similarity') else: RAG = graph.rag_mean_color(img, segments, mode='similarity') if verbose: graph.show_rag(segments, RAG, img, border_color=(1, 0.7, 0)) io.show() foreground_mask = find_foreground(img, segments, RAG, eps=edge_threshold, verbose=verbose) if foreground_mask.any(): vertex, side = fit_square(foreground_mask, verbose=verbose) texture_patch = img[vertex[0]:vertex[0] + side + 1, vertex[1]:vertex[1] + side + 1] if verbose: io.imshow(texture_patch) io.show() return texture_patch else: print('Texture can\'t be generated, try different parameters') return np.array([])
def make_grid_rag(data, res): fig, ax = plt.subplots(len(res), 2) ax = ax.reshape(len(res), 2) # make preview images im = data['image_unnormal'].cpu().numpy() im = [np.rollaxis(im[i, ...], 0, 3) for i in range(im.shape[0])] truth = data['label/segmentation'].cpu().numpy() truth = [truth[i, 0, ...] for i in range(truth.shape[0])] labels = data['labels'].cpu().numpy() labels = [labels[i, ...][0] for i in range(labels.shape[0])] for i, (im_, truth_, labels_, g) in enumerate(zip(im, truth, labels, data['rag'])): truth_contour = segmentation.find_boundaries(truth_, mode='thick') g.add_edges_from( (e[0], e[1], dict(weight=res[i][j].detach().cpu().numpy())) for j, e in enumerate(g.edges)) lc = show_rag(labels_.astype(int), g, im_, ax=ax[i, 1], edge_width=0.5, edge_cmap='viridis') fig.colorbar(lc, ax=ax[i, 1], fraction=0.03) im_[truth_contour, ...] = (1, 0, 0) ax[i, 0].imshow(im_) return fig
def make_grid_rag(im, labels, rag, probas, truth=None): fig = plt.figure() ax = plt.gca() # make preview images rag.add_edges_from([(n0, n1, dict(weight=probas[j])) for j, (n0, n1) in enumerate(rag.edges())]) lc = show_rag(labels.astype(int), rag, im, ax=ax, edge_width=0.5, edge_cmap='viridis') fig.colorbar(lc, ax=ax, fraction=0.03) if (truth is not None): truth_contour = segmentation.find_boundaries(truth, mode='thick') im[truth_contour, ...] = (255, 0, 0) ax.axis('off') ax.imshow(im) fig.tight_layout(pad=0, w_pad=0) fig.canvas.draw() im_plot = np.array(fig.canvas.renderer.buffer_rgba())[..., :3] plt.close(fig) return im_plot
def merge_hier_boundary(labels, image, thresh=0.03, show_rag=False): """ Merges the given labels using a RAG based on boundaries. Parameters ---------- labels: ndarray image: ndarray thresh: float show_rag: bool Returns ------- rag: RAG labels: ndarray Merged labels. """ edges = filters.sobel(color.rgb2gray(image)) rag = graph.rag_boundary(labels, edges) rag_copy = False if show_rag: rag_copy = True fig, ax = plt.subplots(1, 2, figsize=(10, 10)) labels = graph.merge_hierarchical(labels, rag, thresh=thresh, rag_copy=rag_copy, in_place_merge=True, merge_func=merge_boundary, weight_func=weight_boundary) if show_rag: graph.show_rag(labels, rag, image, ax=ax[0]) ax[0].title('Initial RAG') graph.show_rag(labels, graph.rag_boundary(labels, edges), ax=ax[1]) ax[1].title('Final RAG') return rag, labels
def merge_hier_color(labels, image, thresh=0.08, show_rag=False): """ Merges the given labels using a RAG based on the mean color. Parameters ---------- labels: ndarray image: ndarray thresh: float show_rag: bool Returns ------- rag: RAG labels: ndarray Merged labels. """ rag = graph.rag_mean_color(image, labels) rag_copy = False if show_rag: rag_copy = True fig, ax = plt.subplots(1, 2, figsize=(10, 10)) labels = graph.merge_hierarchical(labels, rag, thresh=thresh, rag_copy=rag_copy, in_place_merge=True, merge_func=merge_mean_color, weight_func=_weight_mean_color) # labels2 = graph.cut_normalized(img_slic, rag, thresh=30) # labels2 = graph.cut_threshold(img_slic, rag, 0.2) if show_rag: graph.show_rag(labels, rag, image, ax=ax[0]) ax[0].title('Initial RAG') graph.show_rag(labels, graph.rag_mean_color(image, labels), ax=ax[1]) ax[1].title('Final RAG') return rag, labels
def region_boundry(image): #image = imageGlobal labels = segmentation.slic(image, compactness=30, n_segments=400) edges = filters.sobel(image) edges_rgb = color.gray2rgb(edges) g = graph.rag_boundary(labels, edges) lc = graph.show_rag(labels, g, edges_rgb, img_cmap=None, edge_cmap='viridis', edge_width=1.2) plt.colorbar(lc, fraction=0.03) io.show()
def desenho_rag(rag, labels): ''' Parametro: labels: array 2d grafo Imagem retorno: nada Imprime o grafo em cima da imagem ''' global image lc = graph.show_rag(labels, rag, image) plt.colorbar(lc, fraction=0.03) io.show()
def make_network(mask, draw=False, save_loc=None): orig, image_labels, edge_map = get_img_labels_edge_map(mask) g = graph.rag_boundary(image_labels, edge_map) g.remove_node(0) if draw: fig, ax = plt.subplots(2, 2, figsize=(15, 15), dpi=200) ax = ax.ravel() ax[0].imshow(orig, cmap='gray') ax[2].imshow(label2rgb(image_labels, image=orig)) ax[1].imshow(edge_map) lc = graph.show_rag(image_labels, g, edge_map, ax=ax[3], edge_width=5, edge_cmap='Blues') fig.colorbar(lc, fraction=0.03, ax=ax[3]) pos = {} for idx in list(g.nodes): pos[idx] = (np.array(g.nodes[idx]['centroid'])[::-1]) nx.draw(g, pos, ax=ax[3]) for a in ax: a.grid('off') fig.tight_layout() if save_loc is not None: fig.savefig(save_loc) else: # because if we don't draw then these features aren't added to the graph props = regionprops(image_labels) for (n, data), region, idx in zip(g.nodes(data=True), props, range(len(props))): data['centroid'] = tuple(map(int, region['centroid'])) data['uid'] = idx return g
def region_boundry(img): gimg = color.rgb2gray(img) fig, ax = plt.subplots(nrows=1) labels = segmentation.slic(img, compactness=30, n_segments=400) edges = filters.sobel(gimg) edges_rgb = color.gray2rgb(edges) g = graph.rag_boundary(labels, edges) lc = graph.show_rag(labels, g, edges_rgb, img_cmap=None, edge_cmap='viridis', edge_width=1.2) ax.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) ax.set_title("Original Image") plt.colorbar(lc, fraction=0.03) plt.tight_layout() plt.show()
g = graph.rag_mean_color(p1, labels) # In[33]: labels2 = graph.merge_hierarchical(labels, g, thresh=0.025, rag_copy=True, in_place_merge=True, merge_func=merge_mean_color, weight_func=_weight_mean_color) # In[34]: graph.show_rag(labels, g, p1) # In[35]: plt.figure() out = color.label2rgb(labels2, p1) plt.imshow(out) # In[38]: p2 = np.load('0172ML0009240000104879E01_DRLX.npy', allow_pickle=True) p2 = np.delete(p2, 0, 0) p2.shape # In[39]:
flat = np.ma.MaskedArray(flat, mask) plt.figure(dpi=800) plt.imshow(flat) io.show() edges = filters.sobel(flat) edges_rgb = color.gray2rgb(edges) from skimage.future import graph plt.figure(dpi=800) g = graph.rag_boundary(flat, edges, connectivity=1) g.remove_node(0) lc = graph.show_rag(flat, g, edges_rgb, img_cmap=None, edge_cmap='viridis', edge_width=1.2) plt.colorbar(lc, fraction=0.03) plt.show() for i in range(1, len(uni)): print([uni[x] for x in g.neighbors(i)]) # plt.figure(dpi=500) # plt.scatter(cols[rpos, 0], cols[rpos,1]) # plt.grid(True) # plt.show()
from skimage import data, segmentation from skimage.future import graph import matplotlib.pyplot as plt img = data.coffee() labels = segmentation.slic(img) g = graph.rag_mean_color(img, labels) lc = graph.show_rag(labels, g, img) cbar = plt.colorbar(lc) plt.show()
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Fri Apr 7 10:13:38 2017 @author: luiz """ from skimage.future import graph from skimage import data, segmentation, color, filters, io from matplotlib import pyplot as plt img = data.coffee() gimg = color.rgb2gray(img) labels = segmentation.slic(img, compactness=30, n_segments=400) edges = filters.sobel(gimg) edges_rgb = color.gray2rgb(edges) g = graph.rag_boundary(labels, edges) lc = graph.show_rag(labels, g, edges_rgb, img_cmap=None, edge_cmap='viridis', edge_width=1.2) plt.colorbar(lc, fraction=0.03) io.show()
g = graph.rag_mean_color(p1, segments) # In[9]: labels2 = graph.merge_hierarchical(segments, g, thresh=0.05, rag_copy=True, in_place_merge=True, merge_func=merge_mean_color, weight_func=_weight_mean_color) # In[10]: graph.show_rag(segments, g, p1) # In[11]: plt.figure() out = color.label2rgb(labels2, p1) plt.imshow(out) # In[12]: p2 = np.load('0172ML0009240000104879E01_DRLX.npy', allow_pickle=True) p2 = np.delete(p2, 0, 0) p2.shape # In[13]:
if PLOTS_ON: out_seg = color.label2rgb(segments, img, kind='avg') out_seg_bound = segmentation.mark_boundaries(out_seg, segments, (0, 0, 0)) out_clust = color.label2rgb(labels, img, kind='avg') fig, ax = plt.subplots(nrows=1, ncols=3, sharex=True, sharey=True, figsize=(15, 5)) ax[0].imshow(out_seg) ax[0].set_title('Oversegmentation', fontsize=15) ax1 = graph.show_rag(segments, g, img, border_color=None, img_cmap='gray', edge_cmap='magma', ax=ax[1]) # plt.colorbar(ax1, ax=ax[1]) ax[1].set_title('Region Adjacency Graph', fontsize=15) ax[2].imshow(out_clust) ax[2].set_title('MinCutPool', fontsize=15) for a in ax: a.axis('off') plt.tight_layout() plt.show() # segments = segmentation.felzenszwalb(img, scale=50, sigma=1.5, min_size=50) # out_seg = color.label2rgb(segments, img, kind='avg') # plt.imshow(out_seg)
from skimage.future import graph from skimage import segmentation, color, filters, io from matplotlib import pyplot as plt img = io.imread("images/sample.jpg") gimg = color.rgb2gray(img) labels = segmentation.slic(img, compactness=25, n_segments=800, start_label=1) edges = filters.sobel(gimg) edges_rgb = color.gray2rgb(edges) g = graph.rag_boundary(labels, edges) lc = graph.show_rag(labels, g, edges_rgb, img_cmap=None, edge_cmap='viridis', edge_width=1.1) plt.colorbar(lc, fraction=0.03) io.show()
image2 = resize(image2, (200, 200)) # resize image to fit axis image2 = swirl(image2, rotation=0, strength=10, radius=90) # swirling image2 fig, ax1 = plt.subplots() # creates plot ax1.imshow(image2, cmap=plt.cm.gray, interpolation='none') # displaying image on plot ax1.axis('off') # removing axis and labels plt.show() # displaying whole piece #------------------------------------------------------------------------------------------------- # Example 3 - Creating region boundries around a photo of a cat from skimage.future import graph from skimage import segmentation, color, filters, io image3 = mpimg.imread("cat.jpg") # import photo as NumPy array imagegray = color.rgb2gray(image3) # converts photo to grayscale outline = segmentation.slic( image3, compactness=50, n_segments=1000) # takes grayscaled image and outlines it imagecolor = color.gray2rgb( imagegray ) # converts grayscale image to colored (based on intensity of brightness and darkness) g = graph.rag_boundary(outline, imagegray) # combined graphic broundries lc = graph.show_rag( outline, g, imagecolor, img_cmap=None, edge_cmap='viridis', edge_width=1 ) # graph itself combining color with outline, creating colored outline io.show() # display
from skimage import io, segmentation from skimage.future import graph from matplotlib import pyplot as plt img = io.imread("images/sample.jpg") labels = segmentation.slic(img, compactness=30, n_segments=400, start_label=1) g = graph.rag_mean_color(img, labels) fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True, figsize=(6, 8)) ax[0].set_title('RAG drawn with default settings') lc = graph.show_rag(labels, g, img, ax=ax[0]) # specify the fraction of the plot area that will be used to draw the colorbar fig.colorbar(lc, fraction=0.03, ax=ax[0]) ax[1].set_title('RAG drawn with grayscale image and viridis colormap') lc = graph.show_rag(labels, g, img, img_cmap='gray', edge_cmap='viridis', ax=ax[1]) fig.colorbar(lc, fraction=0.03, ax=ax[1]) for a in ax: a.axis('off') plt.tight_layout() plt.show()
img = data.coffee() labels = [ quickshift(img, kernel_size=3, max_dist=6, ratio=0.5), slic(img, compactness=30, n_segments=400), felzenszwalb(img, scale=100, sigma=0.5, min_size=50) ] label_rgbs = [color.label2rgb(label, img, kind='avg') for label in labels] algos = [["Quickshift", "SLIC (K-Means)", "Felzenszwalb"], [["Quickshift Before RAG", "Quickshift After RAG"], ["SLIC (K-Means) Before RAG", "SLIC (K-Means) After RAG"], ["Felzenszwalb Before RAG", "Felzenszwalb After RAG"]]] rags = [graph.rag_mean_color(img, label) for label in labels] edges_drawn_all = [ plt.colorbar(graph.show_rag(label, rag, img)).set_label(algo) for label, rag, algo in zip(labels, rags, algos[0]) ] for edge_drawn in edges_drawn_all: plt.show() # only display edges with weight > thresh final_labels = [ graph.cut_threshold(label, rag, 29) for label, rag in zip(labels, rags) ] final_label_rgbs = [ color.label2rgb(final_label, img, kind='avg') for final_label in final_labels ]
} def merge_boundary(graph, src, dst): """Call back called before merging 2 nodes. In this case we don't need to do any computation here. """ pass img = data.coffee() edges = filters.sobel(color.rgb2gray(img)) labels = segmentation.slic(img, compactness=30, n_segments=400) g = graph.rag_boundary(labels, edges) graph.show_rag(labels, g, img) plt.title('Initial RAG') labels2 = graph.merge_hierarchical(labels, g, thresh=0.08, rag_copy=False, in_place_merge=True, merge_func=merge_boundary, weight_func=weight_boundary) graph.show_rag(labels, g, img) plt.title('RAG after hierarchical merging') plt.figure() out = color.label2rgb(labels2, img, kind='avg') plt.imshow(out) plt.title('Final segmentation')
def _apply(self, imgmsg, maskmsg): bridge = cv_bridge.CvBridge() img = bridge.imgmsg_to_cv2(imgmsg) if img.ndim == 2: img = gray2rgb(img) mask = bridge.imgmsg_to_cv2(maskmsg, desired_encoding='mono8') mask = mask.reshape(mask.shape[:2]) # compute label roi = closed_mask_roi(mask) roi_labels = masked_slic(img=img[roi], mask=mask[roi], n_segments=20, compactness=30) if roi_labels is None: return labels = np.zeros(mask.shape, dtype=np.int32) # labels.fill(-1) # set bg_label labels[roi] = roi_labels if self.is_debugging: # publish debug slic label slic_labelmsg = bridge.cv2_to_imgmsg(labels) slic_labelmsg.header = imgmsg.header self.pub_slic.publish(slic_labelmsg) # compute rag g = rag_solidity(labels, connectivity=2) if self.is_debugging: # publish debug rag drawn image if LooseVersion(skimage.__version__) >= '0.13.0': fig, ax = plt.subplots(figsize=(img.shape[1] * 0.01, img.shape[0] * 0.01)) show_rag(labels, g, img, ax=ax) ax.axis('off') plt.subplots_adjust(0, 0, 1, 1) fig.canvas.draw() w, h = fig.canvas.get_width_height() rag_img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8) rag_img.shape = (h, w, 3) plt.close() else: rag_img = draw_rag(labels, g, img) rag_img = img_as_uint(rag_img) rag_imgmsg = bridge.cv2_to_imgmsg(rag_img.astype(np.uint8), encoding='rgb8') rag_imgmsg.header = imgmsg.header self.pub_rag.publish(rag_imgmsg) # merge rag with solidity merged_labels = merge_hierarchical(labels, g, thresh=1, rag_copy=False, in_place_merge=True, merge_func=_solidity_merge_func, weight_func=_solidity_weight_func) merged_labels += 1 merged_labels[mask == 0] = 0 merged_labelmsg = bridge.cv2_to_imgmsg(merged_labels.astype(np.int32)) merged_labelmsg.header = imgmsg.header self.pub.publish(merged_labelmsg) if self.is_debugging: out = label2rgb(merged_labels, img) out = (out * 255).astype(np.uint8) out_msg = bridge.cv2_to_imgmsg(out, encoding='rgb8') out_msg.header = imgmsg.header self.pub_label.publish(out_msg)
Drawing Region Adjacency Graphs (RAGs) ====================================== This example constructs a Region Adjacency Graph (RAG) and draws it with the `rag_draw` method. """ from skimage import data, segmentation from skimage.future import graph from matplotlib import pyplot as plt img = data.coffee() labels = segmentation.slic(img, compactness=30, n_segments=400) g = graph.rag_mean_color(img, labels) fig, ax = plt.subplots() ax.set_title('RAG drawn with default settings') lc = graph.show_rag(labels, g, img, ax=ax) # fraction specifies the fraction of the area of the plot that will be used to # draw the colorbar plt.colorbar(lc, fraction=0.03) fig, ax = plt.subplots() ax.set_title('RAG drawn with grayscale image and viridis colormap') lc = graph.show_rag(labels, g, img, img_cmap='gray', edge_cmap='viridis', ax=ax) plt.colorbar(lc, fraction=0.03) plt.show()