## 图像分割的概念

import numpy as npfrom skimage.transform import (hough_line, hough_line_peaks, hough_circle,hough_circle_peaks)from skimage.draw import circle_perimeterfrom skimage.feature import cannyfrom skimage.data import astronautfrom skimage.io import imread, imsavefrom skimage.color import rgb2gray, gray2rgb, label2rgbfrom skimage import img_as_floatfrom skimage.morphology import skeletonizefrom skimage import data, img_as_floatimport matplotlib.pyplot as pylabfrom matplotlib import cmfrom skimage.filters import sobel, threshold_otsufrom skimage.feature import cannyfrom skimage.segmentation import felzenszwalb, slic, quickshift, watershedfrom skimage.segmentation import mark_boundaries, find_boundaries

coins = data.coins()hist = np.histogram(coins, bins=np.arange(0, 256), normed=True)fig, axes = pylab.subplots(1, 2, figsize=(20, 10))axes[0].imshow(coins, cmap=pylab.cm.gray, interpolation='nearest')axes[0].axis('off'), axes[1].plot(hist[1][:-1], hist[0], lw=2)axes[1].set_title('histogram of gray values')pylab.show()

## 8.4.1　基于边缘的图像分割

edges = canny(coins, sigma=2)fig, axes = pylab.subplots(figsize=(10, 6))axes.imshow(edges, cmap=pylab.cm.gray, interpolation='nearest')axes.set_title('Canny detector'), axes.axis('off'), pylab.show()

from scipy import ndimage as ndifill_coins = ndi.binary_fill_holes(edges)fig, axes = pylab.subplots(figsize=(10, 6))axes.imshow(fill_coins, cmap=pylab.cm.gray, interpolation='nearest')axes.set_title('filling the holes'), axes.axis('off'), pylab.show()

from skimage import morphologycoins_cleaned = morphology.remove_small_objects(fill_coins, 21)fig, axes = pylab.subplots(figsize=(10, 6))axes.imshow(coins_cleaned, cmap=pylab.cm.gray, interpolation='nearest')axes.set_title('removing small objects'), axes.axis('off'), pylab.show()

## 形态学分水岭算法

（1）找到标记和分割准则（用于分割区域的函数，通常是图像对比度/梯度）；

（2）利用这两个元素运行标记控制的分水岭算法。

elevation_map = sobel(coins)fig, axes = pylab.subplots(figsize=(10, 6))axes.imshow(elevation_map, cmap=pylab.cm.gray, interpolation='nearest')axes.set_title('elevation map'), axes.axis('off'), pylab.show()

markers = np.zeros_like(coins)markers[coins < 30] = 1markers[coins > 150] = 2print(np.max(markers), np.min(markers))fig, axes = pylab.subplots(figsize=(10, 6))a = axes.imshow(markers, cmap=plt.cm.hot, interpolation='nearest')plt.colorbar(a)axes.set_title('markers'), axes.axis('off'), pylab.show()

segmentation = morphology.watershed(elevation_map, markers)fig, axes = pylab.subplots(figsize=(10, 6))axes.imshow(segmentation, cmap=pylab.cm.gray, interpolation='nearest')axes.set_title('segmentation'), axes.axis('off'), pylab.show()

segmentation = ndi.binary_fill_holes(segmentation - 1)labeled_coins, _ = ndi.label(segmentation)image_label_overlay = label2rgb(labeled_coins, image=coins)fig, axes = pylab.subplots(1, 2, figsize=(20, 6), sharey=True)axes[0].imshow(coins, cmap=pylab.cm.gray, interpolation='nearest')axes[0].contour(segmentation, [0.5], linewidths=1.2, colors='y')axes[1].imshow(image_label_overlay, interpolation='nearest')for a in axes: a.axis('off')pylab.tight_layout(), pylab.show()