def preview(self, ips, para): lab = self.app.get_img(para['lab']).img connect = ['4-connected', '8-connected'].index(para['connect']) + 1 g = graph.rag_mean_color(ips.snap, lab, connect, para['mode'], para['sigma']) lab = graph.cut_normalized(lab, g, para['thresh'], para['num']) ips.img[:] = color.label2rgb(lab, ips.snap, kind='avg')
def graphNormalizedCuts(self,img, labels,thresh=0.5,num_cuts=100,plot=True,save=0): g = graph.rag_mean_color(img, labels) new_labels = graph.cut_normalized(labels, g,thresh,num_cuts) if plot: plotLibs().dispImg(color.label2rgb(new_labels, img, kind='avg'),save=save,title='Ncut Label rgb Image') plotLibs().plotBoundaries(img,new_labels,save=save,title='Ncut Boundary Images') return new_labels
def normalized_graphcut(self, s_labels): start_time = time.time() _graph = graph.rag_mean_color(self.q_cur_frame, s_labels, mode="similarity") labels = graph.cut_normalized(s_labels, _graph) # self.c_frame = color.label2rgb(labels,self.q_cur_frame, kind='avg') cv2.imwrite("oucut.png", self.s_frame) print "Graph N Cut(preprocess) : ", time.time() - start_time
def quantizeWithoutPos(im, img_base_name, output_dir): labels1 = run_k_means(im, compactness=30, n_segments=400) g = graph.rag_mean_color(im, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g) img_name = os.path.splitext(img_base_name)[0] save_as_mat_file(labels2, img_name, output_dir)
def superpixel(cv2_img, n_segments=1111, compactness=10, normalized_cut=False, debug=True): """ parameters like 1111 and 10 are selected by my intuition :param cv2_img: cv2.imread('../example_images/2007_000039.jpg') :param debug: print debug info :return: labels: [h,w] numpy array, unique_ids: [0, 1, ..., superpixel_n - 1] """ # labels = segmentation.slic(cv2_img, n_segments=1111, compactness=10) labels = segmentation.slic(cv2_img, n_segments=n_segments, compactness=compactness) if normalized_cut: g = graph.rag_mean_color(cv2_img, labels, mode='similarity') labels = graph.cut_normalized(labels, g) unique_ids = np.unique(labels) logger.debug('this image has {} unique superpixels'.format( len(unique_ids))) return labels, unique_ids
def probabilitygraph(rgb): # print rgb.shape rgb = rgb[0] # print bottom[0][:,:,0].shape # rgb[0] = (rgb[0]-np.min(rgb[0]))/(np.max(rgb[0])-np.min(rgb[0])) # rgb[1] = (rgb[1]-np.min(rgb[1]))/(np.max(rgb[1])-np.min(rgb[1])) # rgb[2] = (rgb[2]-np.min(rgb[2]))/(np.max(rgb[2])-np.min(rgb[2])) # bottom[0][:,:,0] = preprocessing.normalize(bottom[0][:,:,0]) # bottom[0][:,:,1] = preprocessing.normalize(bottom[0][:,:,1]) # bottom[0][:,:,2] = preprocessing.normalize(bottom[0][:,:,2]) img = rgb #bottom[0].data # print "img", img.shape # print img img = np.transpose(img,(1,2,0)) img = np.array(img,dtype=np.float) # print img # print img.shape labels1 = segmentation.slic(img, compactness=30, n_segments=400) # print "labels1",labels1 # out1 = color.label2rgb(labels1, img, kind='avg') # print labels1.shape g = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g) out2 = color.label2rgb(labels2, img, kind='avg') # print "out2,",out2 return out2
def _ncut_seg(self, data_list): """ Use the ncut method to segment the image [image, slic_label/[slic_param], [ncut_param]] """ img = data_list[0] param = data_list[1] param_cut = data_list[2] if param_cut is None: threahold = 0.001 else: threahold = param_cut[0] # Check if the param is the super pixel label or the num of super pixel # to be segmented try: num = int(param[0]) # super pixel seg label1 = segmentation.slic(img, compactness=10, n_segments=num, slic_zero=True) except: label1 = param # N-Cut g = graph.rag_mean_color(img, label1, mode='similarity') try: label2 = graph.cut_normalized(label1, g, thresh=threahold) except: log.error( '\033[01;31mERROR\033[0m: Unknow Error in cut_normalized \ function.') label2 = np.zeros(label1.shape).astype('int') return label2
def segment(self, compactness=50, n_segments=100, connectivity=1, sigma=500, num_cuts=200): """ Function responable of image segmentation """ if self.segments is None: # Make segmentation slice labels1 = segmentation.slic(self.image, compactness=compactness, n_segments=n_segments) # Convert slice into rgb image based on avg out1 = color.label2rgb(labels1, self.image, kind='avg') # Complete segmentation using graph cut g = graph.rag_mean_color(self.image, labels1, mode='similarity', connectivity=connectivity, sigma=sigma) labels = graph.cut_normalized(labels1, g, num_cuts=num_cuts) image_segmentation = color.label2rgb(labels, self.image, kind='avg') self._set_boxes(labels)
def _segment(filename): with rasterio.open(filename) as src: slic_params = { 'compactness': 20, 'n_segments': 200, 'multichannel': True } # Segment the image. rout = dp.segmentation(model=slic, params=slic_params, src=src, modal_radius=3) # Region Agency Graph to merge segments orig = dp.bsq_to_bip(src.read([1, 2, 3], masked=True)) labels = (dp.bsq_to_bip(rout))[:, :, 0] rag = graph.rag_mean_color(orig, labels, mode='similarity') rout = graph.cut_normalized(labels, rag) # Vectorize the RAG segments rout = dp.bip_to_bsq(rout[:, :, np.newaxis]) vout = dp.vectorize(image=rout, transform=src.transform, crs=src.crs.to_proj4()) # Add spectral properties. vout = dp.add_zonal_properties(src=src, bands=[1, 2, 3], band_names=['red', 'green', 'blue'], stats=['mean', 'min', 'max', 'std'], gdf=vout) # Add shape properties. vout = dp.add_shape_properties(rout, vout, [ 'area', 'perimeter', 'eccentricity', 'equivalent_diameter', 'major_axis_length', 'minor_axis_length', 'orientation' ]) # Add texture properties. edges = dp.sobel_edge_detect(src, band=1) vout = dp.add_zonal_properties(image=edges, band_names=['sobel'], stats=['mean', 'min', 'max', 'std'], transform=src.transform, gdf=vout) ################### # Temporary code to write to vector and raster formats... # for checking output. Akin to using print statements to debug. #vout.to_file("output/working.shp") #out_raster = dp.rasterize(vout, 'dn', src.shape, src.transform) #utility.write_geotiff(out_raster[np.newaxis, :], 'rasterized.tif', # src, count=1, dtypes=('uint8')) ################### return vout
def normalizedCut(testRGBImage,imagePath,clustersLabels,groundTruthLabelVector,k): image = mpimg.imread(imagePath) #Compute the Region Adjacency Graph g = graph.rag_mean_color(testRGBImage, np.reshape(clustersLabels,(nrows,ncols)), mode='similarity') #Perform Normalized Graph cut on the Region Adjacency Graph. labels = graph.cut_normalized(np.reshape(clustersLabels,(nrows,ncols)), g) #return labels return labels
def normalized_graphcut(self, s_labels): start_time = time.time() _graph = graph.rag_mean_color(self.q_cur_frame, s_labels, mode='similarity') labels = graph.cut_normalized(s_labels, _graph) #self.c_frame = color.label2rgb(labels,self.q_cur_frame, kind='avg') cv2.imwrite('oucut.png', self.s_frame) print "Graph N Cut(preprocess) : ", time.time() - start_time
def create_mask(filename, n_segments=400, n_cuts=10): img = data.load(filename) labels1 = segmentation.slic(img, n_segments=n_segments) rag = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, rag, num_cuts=n_cuts) return labels2, img
def get_rag_cuts(img): labels1 = segmentation.slic(img, compactness=30, n_segments=400) out1 = color.label2rgb(labels1, img, kind='avg') g = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g) out2 = color.label2rgb(labels2, img, kind='avg') return labels1, labels2
def ncut_image(image): image = cv2.resize(image, (160, 128)) labels1 = segmentation.slic(image, compactness=30, n_segments=400) g = graph.rag_mean_color(image, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g, thresh=0.05, num_cuts=10) out2 = skimage.color.label2rgb(labels2, image, kind='avg') lenn = len(glob.glob('keys/objs/*.jpg')) cv2.imwrite('keys/objs/' + str(lenn + 1) + '.jpg', out2) return labels2
def image_segmentation(self, **kwargs): """Compute low-level segmentation methods like felzenswalb'efficient graph based segmentation or k-means based image segementation https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_segmentations.html#sphx-glr-auto-examples-segmentation-plot-segmentations-py """ # get settings of combobox and fields param = self._csbox_seg.get_dict() # get the currently displayed image img = self.get_obj().get_img(show=True) if param["Model"]=="SLIC" or param["Model"]=="Normalized Cuts": # https://scikit-image.org/docs/dev/api/skimage.segmentation.html#skimage.segmentation.slic # n_segments = the (approximate) number of labels in the segmented output image. # compactness: balances color proximity and space proximity. # max_iter: maximum number of iterations of k-means. seg_map = segmentation.slic(img, **self._csbox_slic.get_dict(), start_label=1, convert2lab=1) seg_map_bound = segmentation.mark_boundaries(img, seg_map) seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0) img_list = [seg_map_bound, seg_map_color] if param["Model"]=="Normalized Cuts": # https://scikit-image.org/docs/stable/api/skimage.future.graph.html#skimage.future.graph.cut_normalized # https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_ncut.html g = graph.rag_mean_color(img, seg_map, mode='similarity') seg_map = graph.cut_normalized(seg_map, g) seg_map_bound = segmentation.mark_boundaries(img, seg_map) seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0) img_list.extend([seg_map_bound, seg_map_color]) elif param["Model"]=="GrabCut": # https://docs.opencv.org/master/dd/dfc/tutorial_js_grabcut.html # get settings of combobox and fields iterCount = self._csbox_grab.get_dict()["iterCount"] # get the region of interest roi = self.get_obj().get_roi() # raise error if the width and height of the roi is not defined if not sum(roi[2:4]): raise IndexError("There are no images available.") # allocate mask, background and foreground model mask = np.zeros(img.shape[:2],np.uint8) bgdModel = np.zeros((1,65),np.float64) fgdModel = np.zeros((1,65),np.float64) # implement the grabcut algorithm and assign the result of the algorithm to variable img_cut # define image list for visualization img_list = [img, img_cut, img[roi[1]:roi[1]+roi[3], roi[0]:roi[0]+roi[2], :]] # open a topwindow with the segmentation results of the currently displayed image tw.TopWindow(self, title="Segmentation", dtype="img", value=img_list)
def image_segmentation(self, **kwargs): """Compute low-level segmentation methods like felzenswalb' efficient graph based segmentation or k-means based image segementation https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_segmentations.html#sphx-glr-auto-examples-segmentation-plot-segmentations-py """ # get settings of combobox and fields param = self._csbox_seg.get_dict() # get the currently displayed image img = self.get_obj().get_img() # define image list for visualization img_list = [img] if param["Model"]=="SLIC": # https://scikit-image.org/docs/dev/api/skimage.segmentation.html#skimage.segmentation.slic # n_segments = the (approximate) number of labels in the segmented output image. # compactness: balances color proximity and space proximity. # max_iter: maximum number of iterations of k-means. seg_map = segmentation.slic(img, **self._csbox_slic.get_dict(), start_label=1) seg_map_bound = segmentation.mark_boundaries(img, seg_map) seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0) # define image list for visualization img_list.extend([seg_map_bound, seg_map_color]) elif param["Model"]=="Felzenswalb": # https://scikit-image.org/docs/dev/api/skimage.segmentation.html#skimage.segmentation.felzenszwalb. seg_map = segmentation.felzenszwalb(img, **self._csbox_felz.get_dict()) seg_map_bound = segmentation.mark_boundaries(img, seg_map) seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0) # define image list for visualization img_list.extend([seg_map_bound, seg_map_color]) elif param["Model"]=="Normalized Cuts": # https://scikit-image.org/docs/stable/api/skimage.future.graph.html#skimage.future.graph.cut_normalized # https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_ncut.html seg_map = segmentation.slic(img, **self._csbox_slic.get_dict(), start_label=1) seg_map_bound = segmentation.mark_boundaries(img, seg_map) seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0) g = graph.rag_mean_color(img, seg_map, mode='similarity') seg_map = graph.cut_normalized(seg_map, g) seg_map_bound = segmentation.mark_boundaries(img, seg_map) seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0) # define image list for visualization img_list.extend([seg_map_bound, seg_map_color]) # open a topwindow with the segmentation results of the currently displayed image self._img_tw = tw.TopWindow(self, title="Segmentation", dtype="img", value=img_list) self._img_seg = img self._seg_map = seg_map
def segment(img_path, node_num): """ Segment an image to multiple graphs. Parameters --- img_path: `str` node_num: `int` Returns --- graphs: `MultiGraph` """ img = imread(img_path) # Segment the image to node_num picese (approximately). nodes = slic(img, sigma=1, n_segments=node_num) # true_node_num = len(np.unique(nodes)) nodes_colors = color.label2rgb(nodes, img, kind='avg') # avg color for each node # Segment nodes to regions (graphs) based on Normalized Cut. regions = graph.cut_normalized( nodes, graph.rag_mean_color(img, nodes, mode='similarity')) # region_num = len(np.unique(regions)) region_set = np.unique(regions) # Build multi-graphs graphs = MultiGraph() for i in region_set: region_graph = Graph() x_len = len(regions) for x in range(x_len): y_len = len(regions[x]) for y in range(y_len): if regions[x][y] == i: node_id = nodes[x][y] # Add nodes # 4-bit (16 colors) r = int(nodes_colors[x][y][0] / 256 * 16) g = int(nodes_colors[x][y][1] / 256 * 16) b = int(nodes_colors[x][y][2] / 256 * 16) label = r + (g << 4) + (b << 8) region_graph.add_node(node_id, label) # Add edges for xx, yy in [(x, y + 1), (x, y - 1), (x + 1, y), (x - 1, y)]: if xx < x_len and xx > 0 and yy < y_len and yy > 0 and regions[ xx][yy] == i: region_graph.add_edge(node_id, nodes[xx][yy]) graphs.add_graph(region_graph) return graphs
def run_ncut(G, labels2_map, ncut_map, nodelist, ncut_threshold): start_t = time.time() labels = np.array([i for i in G.nodes_iter()]) labels2 = graph.cut_normalized(labels, G, thresh=ncut_threshold) for i1, i2 in itertools.izip(labels, labels2): labels2_map[i2].append(nodelist[i1]) ncut_map[i1] = i2 print >> sys.stderr, "(subgraph) has {0} nodes. ncut down to {1} partitions in {2} sec.".format(\ G.number_of_nodes(), len(set(labels2)), time.time()-start_t)
def run_ncut(G, labels2_map, ncut_map, nodelist): start_t = time.time() labels = np.array([i for i in G.nodes_iter()]) labels2 = graph.cut_normalized(labels, G, thresh=0.2) for i1, i2 in itertools.izip(labels, labels2): labels2_map[i2].append(nodelist[i1]) ncut_map[i1] = i2 print >> sys.stderr, "(subgraph) has {0} nodes. ncut down to {1} partitions in {2} sec.".format(\ G.number_of_nodes(), len(set(labels2)), time.time()-start_t)
def plotNcut(img): """ img: color img """ labels1 = segmentation.slic(img, compactness=30, n_segments=400) g = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g) return color.label2rgb(labels2, img, kind='avg'), labels2
def normalized_cut(img): labels1 = slic(img, compactness=50, n_segments=1000) g = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g) out2 = color.label2rgb(labels2, img, kind='avg') # print(np.mean(out2)) # check for inverted colors if np.mean(out2) > 100: out2 = 200 - out2 return out2
def grafo(img): labels = segmentation.slic(gaussian(img, 5), n_segments=20, compactness=10, multichannel=True) rag = graph.rag_mean_color(img, labels, mode='similarity') new_labels = graph.cut_normalized(labels, rag) # rec = segmentation.mark_boundaries(img, new_labels) # new_labels = color.label2rgb(new_labels) return new_labels
def test_inplace(): """Make sure normalized cuts does not modify arguments when in_place=False""" img = data.coffee() labels1 = segmentation.slic(img, compactness=30, n_segments=400) g = graph.rag_mean_color(img, labels1, mode='similarity') backup_labels = deepcopy(labels1) backup_g = deepcopy(g) _ = graph.cut_normalized(labels1, g, in_place=False, thresh=1e-8) assert_array_equal(backup_labels, labels1) assert backup_g == g
def normailsed_cuts(img): img = img.astype(np.float64) / img.max() img = 255 * img # Now scale by 255 img = img.astype(np.uint8) labels1 = segmentation.slic(img,n_segments=4, compactness=30) out1 = color.label2rgb(labels1, img, kind='avg') g = graph.rag_mean_color(img, labels1, mode='similarity') labels_normalised = graph.cut_normalized(labels1, g) colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (0,1,1)] out_normalised = color.label2rgb(labels_normalised, img, kind='overlay',colors=colors) return out_normalised, labels_normalised
def normalized_cut( img, ncut = 10 ): ''' segment image by normalized_cut ''' labels1 = segmentation.slic(img, convert2lab = True, compactness=40, n_segments=400) # out1 = color.label2rgb(labels1, img, kind='avg') g = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g, num_cuts=ncut) # out2 = color.label2rgb(labels2, img, kind='avg') return labels2
def clustering_RAG(img, op="disc", test=False): to_plot = [] img_red = img[:, :, 0] if test: to_plot.append(("red_chan", img)) (ancho, alto) = img_red.shape rr, cc = ellipse(ancho / 2, alto / 2, (ancho / 2), (alto / 2)) mask_background = np.zeros((ancho, alto)) > 0 mask_background[rr, cc] = 1 """ Clustering k-means type """ if op == "disc": labels1 = segmentation.slic(img, mask=mask_background, n_segments=250, compactness=15, sigma=1, start_label=1) out1 = color.label2rgb(labels1, img) if test: to_plot.append(("Cluster1", out1)) g = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g) if op == "cup": labels1 = segmentation.slic(img, mask=mask_background, n_segments=100, compactness=10, sigma=1, start_label=1) out1 = color.label2rgb(labels1, img) if test: to_plot.append(("Cluster2", out1)) g = graph.rag_mean_color(img, labels1) g = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_threshold(labels1, g, 500) out2 = color.label2rgb(labels2, img) if test: to_plot.append(("RAGs", out2)) if test: vi.plot_multy(to_plot, 1, 3, 'K-means + RAGs')
def norm_grap_cut(image, max_edge=10000000, max_rec=4, compactness=2, nrSupPix=2000): """Normalized graph cut wrapper for 2D numpy arrays. Parameters ---------- image: np.ndarray (2D) Volume histogram. max_edge: float The maximum possible value of an edge in the RAG. This corresponds to an edge between identical regions. This is used to put self edges in the RAG. compactness: float From skimage slic_superpixels.py slic function: Balances color proximity and space proximity. Higher values give more weight to space proximity, making superpixel shapes more square/cubic. This parameter depends strongly on image contrast and on the shapes of objects in the image. nrSupPix: int, positive The (approximate) number of superpixels in the region adjacency graph. Returns ------- labels2, labels1: np.ndarray (2D) Segmented volume histogram mask image. Each label has a unique identifier. """ # scale for uint8 conversion image = np.round(255 / image.max() * image) image = image.astype('uint8') # scikit implementation expects rgb format (shape: NxMx3) image = np.tile(image, (3, 1, 1)) image = np.transpose(image, (1, 2, 0)) labels1 = slic(image, compactness=compactness, n_segments=nrSupPix, sigma=2) # region adjacency graph (rag) g = graph.rag_mean_color(img, labels1, mode='similarity_and_proximity') labels2 = graph.cut_normalized(labels1, g, max_edge=max_edge, num_cuts=1000, max_rec=max_rec) return labels2, labels1
def create_mask(filename, n_segments=400, n_cuts=10): img = io.imread(filename) if img.shape[2] == 4: img = img_as_ubyte(color.rgba2rgb(img)) labels1 = segmentation.slic(img, n_segments=n_segments) rag = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, rag, num_cuts=n_cuts) return labels2, img
def test_reproducibility(): """ensure cut_normalized returns the same output for the same input, when specifying random_state """ img = data.coffee() labels1 = segmentation.slic(img, compactness=30, n_segments=400) g = graph.rag_mean_color(img, labels1, mode='similarity') results = [None] * 4 for i in range(len(results)): results[i] = graph.cut_normalized( labels1, g, in_place=False, thresh=1e-3, random_state=1234) for i in range(len(results) - 1): assert_array_equal(results[i], results[i + 1])
def test_ncut_stable_subgraph(): """ Test to catch an error thrown when subgraph has all equal edges. """ img = np.zeros((100, 100, 3), dtype='uint8') labels = np.zeros((100, 100), dtype='uint8') labels[:50, :50] = 1 labels[:50, 50:] = 2 rag = graph.rag_mean_color(img, labels, mode='similarity') new_labels = graph.cut_normalized(labels, rag, in_place=False) new_labels, _, _ = segmentation.relabel_sequential(new_labels) assert new_labels.max() == 0
def run(self, ips, imgs, para=None): if not para['stack']: imgs, labs = [ips.img], [self.app.get_img(para['lab']).img] else: labs = self.app.get_img(para['lab']).imgs if len(imgs) != len(labs): labs = [self.app.get_img(para['lab']).img] * len(imgs) for i in range(len(imgs)): img, lab = imgs[i], labs[i] connect = ['4-connected', '8-connected'].index(para['connect']) + 1 g = graph.rag_mean_color(img, lab, connect, para['mode'], para['sigma']) lab = graph.cut_normalized(lab, g, para['thresh'], para['num']) img[:] = color.label2rgb(lab, img, kind='avg') self.progress(i, len(imgs))
def test_cut_normalized(): img = np.zeros((100, 100, 3), dtype='uint8') img[:50, :50] = 255, 255, 255 img[:50, 50:] = 254, 254, 254 img[50:, :50] = 2, 2, 2 img[50:, 50:] = 1, 1, 1 labels = np.zeros((100, 100), dtype='uint8') labels[:50, :50] = 0 labels[:50, 50:] = 1 labels[50:, :50] = 2 labels[50:, 50:] = 3 rag = graph.rag_mean_color(img, labels, mode='similarity') new_labels = graph.cut_normalized(labels, rag, in_place=False) new_labels, _, _ = segmentation.relabel_sequential(new_labels) # Two labels assert new_labels.max() == 1 new_labels = graph.cut_normalized(labels, rag) new_labels, _, _ = segmentation.relabel_sequential(new_labels) assert new_labels.max() == 1
def tf_seg_normalized_cut(imgpath, tffolder, n_segments=400, compactness=1, sigma=100): img = io.imread(imgpath) label_km = segmentation.slic(img, n_segments=n_segments, compactness=compactness) g = graph.rag_mean_color(img, label_km, mode='similarity', sigma=sigma) # create the region adjacency graph # sigma controls color similarity, higher, more similar, fewer regions label = graph.cut_normalized(label_km, g) save_label(imgpath, tffolder, label, "segment normalized_cut") return (label)
label1_rgb = segmentation.mark_boundaries(labels1_rgb, labels1, (0, 0, 0)) show_img(label1_rgb) # RAG graph for the first segmentation rag = rag_mean_color(img, labels1) for region in regions1: rag.node[region['label']]['centroid'] = region['centroid'] #labels1 = cut_threshold(segments_slic, g) edges_drawn_all = display_edges(label1_rgb, rag, 20 ) show_img(edges_drawn_all) labels2 = cut_normalized(labels1, rag) labels2 = labels2 + 1 #regions2 = regionprops(labels2) labels2_rgb = color.label2rgb(labels2, img, kind='avg') show_img(labels2_rgb) #for region in regions2: # rag.node[region['label']]['centroid'] = region['centroid'] #edges_drawn_l2 = display_edges(labels2, rag, 50 ) #show_img(edges_drawn_l2) #print("Felzenszwalb's number of segments: %d" % len(np.unique(segments_fz))) print("Slic number of segments: %d" % len(np.unique(segments_slic)))
for image_name in image_names: print("Processing image:", image_name) image = io.imread(image_name + ".jpg") ms_output = {} for b in bandwidths: for r in radius: eps = 1 new_image = mean_shift(image, radius=r, bandwidth=b, eps=eps) ms_output[b] = new_image io.imsave(image_name + "_ms_local_b" + str(b) + "_r" + str(r) + "_e" + str(eps) + "_floodfill.jpg", new_image) print("Computing N-Cut ...") label_slic = segmentation.slic(image, compactness=20, n_segments=600) mean = graph.rag_mean_color(image, label_slic, mode='similarity') label_ncut = graph.cut_normalized(label_slic, mean) ncut_output = color.label2rgb(label_ncut, image, kind='avg') io.imsave(image_name + "_ncut.jpg", ncut_output) # plt.figure().suptitle('Original') # io.imshow(image) # # for b in bandwidths: # plt.figure().suptitle('Result of Mean Shift - Bandwidth = ' + str(b)) # io.imshow(ms_output[b]) # # plt.figure().suptitle('Result of N-Cut') # io.imshow(ncut_output) # io.show()
for edge in g.edges_iter(): n1, n2 = edge r1, c1 = map(int, g.node[n1]['centroid']) r2, c2 = map(int, g.node[n2]['centroid']) n_green = np.array([0, 1, 0]) n_red = np.array([1, 0, 0]) line = draw.line(r1, c1, r2, c2) circle = draw.circle(r1, c1, 2) norm_weight = (g[n1][n2]['weight']-min_weight)/(max_weight-min_weight) image[line] = norm_weight*n_red + (1-norm_weight)*n_green image[circle] = 1, 1, 0 #the center of the node return image if __name__ == '__main__': img = data.coffee() # img = data.camera() labels1 = segmentation.slic(img, compactness=30, n_segments=120000) out1 = color.label2rgb(labels1, img, kind='avg') show_img(out1) g = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g) print labels2 out2 = color.label2rgb(labels2, img, kind='avg') show_img(out2)