""" Visiualize segmentation contours on original grayscale image. """ from skimage import data, segmentation, filter, color import matplotlib.pyplot as plt coins = data.coins() mask = coins > filter.threshold_otsu(coins) clean_border = segmentation.clear_border(mask) coins_edges = segmentation.visualize_boundaries(color.gray2rgb(coins), clean_border) plt.figure(figsize=(8, 3.5)) plt.subplot(121) plt.imshow(clean_border, cmap='gray') plt.axis('off') plt.subplot(122) plt.imshow(coins_edges) plt.axis('off') plt.tight_layout() plt.show()
# -*- coding: cp936 -*- """ Created on Tue Dec 09 16:14:53 2014 @author: shuaiyi """ import skimage.segmentation as seg import skimage.io as io import matplotlib.pyplot as plt img = io.imread("test.png") ax = plt.figure() segments_slic = seg.slic(img, n_segments=1000, compactness=10, sigma=1) plt.imshow(seg.visualize_boundaries(img, segments_slic)) # for fun # def f(n): # if n == 1: # return 1000*1.047 # return (f(n-1)+1000)*1.047
# -*- coding: cp936 -*- """ Created on Tue Dec 09 16:14:53 2014 @author: shuaiyi """ import skimage.segmentation as seg import skimage.io as io import matplotlib.pyplot as plt img = io.imread('test.png') ax = plt.figure() segments_slic = seg.slic(img, n_segments=1000, compactness=10, sigma=1) plt.imshow(seg.visualize_boundaries(img, segments_slic)) #for fun #def f(n): # if n == 1: # return 1000*1.047 # return (f(n-1)+1000)*1.047
import numpy as np from skimage.data import lena from skimage.segmentation import felzenszwalb, \ visualize_boundaries, slic, quickshift from skimage.util import img_as_float img = img_as_float(lena()[::2, ::2]) segments_fz = felzenszwalb(img, scale=100, sigma=0.5, min_size=50) segments_slic = slic(img, ratio=10, n_segments=250, sigma=1) segments_quick = quickshift(img, kernel_size=3, max_dist=6, ratio=0.5) print("Felzenszwalb's number of segments: %d" % len(np.unique(segments_fz))) print("Slic number of segments: %d" % len(np.unique(segments_slic))) print("Quickshift number of segments: %d" % len(np.unique(segments_quick))) fig, ax = plt.subplots(1, 3) fig.set_size_inches(8, 3, forward=True) plt.subplots_adjust(0.05, 0.05, 0.95, 0.95, 0.05, 0.05) ax[0].imshow(visualize_boundaries(img, segments_fz)) ax[0].set_title("Felzenszwalbs's method") ax[1].imshow(visualize_boundaries(img, segments_slic)) ax[1].set_title("SLIC") ax[2].imshow(visualize_boundaries(img, segments_quick)) ax[2].set_title("Quickshift") for a in ax: a.set_xticks(()) a.set_yticks(()) plt.show()
k["Y"] = Y del k['Centroid'] fieldnames = ['Label', 'MeanIntensity', 'Y', 'X', 'Area', 'Perimeter','MajorAxisLength'] output_results = dir_output+seg+ws+str(i)+'_results.txt' test_file = open(output_results,'wb') csvwriter = csv.DictWriter(test_file, delimiter='\t', fieldnames=fieldnames) csvwriter.writerow(dict((fn,fn) for fn in fieldnames)) for row in props_final: csvwriter.writerow(row) test_file.close() # find outline of objects for plotting boundaries = find_boundaries(labelled_image) img_rgb = gray2rgb(image) overlay = np.flipud(visualize_boundaries(img_rgb,boundaries)) # plot overlay between original picture and identified objects # coordinates are delivered as tuples with first height and then width, opposite to plotting first x (width) and then y (height) coords_rev = [(b, a) for a, b in coords] rcParams['savefig.dpi'] = 700 fig = figure(figsize=(9.36, 6.24)) canvas = FigureCanvas(fig) plot = fig.add_subplot(111) for label, xy in zip(object_label, coords_rev): plot.annotate(label,xy=xy,xytext=None,ha='center',va='center',color='red',fontsize=1) plot.imshow(np.flipud(overlay)) height, width = image.shape plot.set_xlim(0, width) plot.set_ylim(0, height) plot.get_xaxis().set_visible(False)