群聚法 Clustering - EX 12:Spectral clustering for image segmentation

优质
小牛编辑
121浏览
2023-12-01

http://scikit-learn.org/stable/auto_examples/cluster/plot_segmentation_toy.html

此范例是利用Spectral clustering来区别重叠的圆圈,将重叠的圆圈分为个体。

  1. 建立一个100x100的影像包含四个不同半径的圆
  2. 透过np.indices改变影像颜色复杂度
  3. spectral_clustering区分出各个不同区域特征

(一)引入函式库

引入函式库如下:

  1. numpy:产生阵列数值
  2. matplotlib.pyplot:用来绘制影像
  3. sklearn.feature_extraction import image:将每个像素的梯度关係图像化
  4. sklearn.cluster import spectral_clustering:将影像正规化切割
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn.feature_extraction import image
  4. from sklearn.cluster import spectral_clustering

(二)建立要被区分的重叠圆圈影像

  • 产生一个大小为输入值得矩阵(此范例为100x100),其内部值为沿着座标方向递增(如:0,1,…)的值。
  1. l = 100
  2. x, y = np.indices((l, l))
  • 建立四个圆圈的圆心座标并给定座标值
  • 给定四个圆圈的半径长度
  • 将圆心座标与半径结合产生四个圆圈图像
  1. center1 = (28, 24)
  2. center2 = (40, 50)
  3. center3 = (67, 58)
  4. center4 = (24, 70)
  5. radius1, radius2, radius3, radius4 = 16, 14, 15, 14
  6. circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1 ** 2
  7. circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2 ** 2
  8. circle3 = (x - center3[0]) ** 2 + (y - center3[1]) ** 2 < radius3 ** 2
  9. circle4 = (x - center4[0]) ** 2 + (y - center4[1]) ** 2 < radius4 ** 2
  • 将上一段产生的四个圆圈影像合併为img使其成为一体的物件
  • mask为布林形式的img
  • img为浮点数形式的img
  • 用乱数产生的方法将整张影像作乱数处理
  1. # 4 circles
  2. img = circle1 + circle2 + circle3 + circle4
  3. mask = img.astype(bool)
  4. img = img.astype(float)
  5. img += 1 + 0.2 * np.random.randn(*img.shape)

接着将产生好的影像化为可使用spectral_clustering的影像

  • image.img_to_graph 用来处理边缘的权重与每个像速间的梯度关联有关
  • 用类似Voronoi Diagram演算法的概念来处理影像
  1. graph = image.img_to_graph(img, mask=mask)
  2. graph.data = np.exp(-graph.data / graph.data.std())

最后用spectral_clustering将连在一起的部分切开,而spectral_clustering中的各项参数设定如下:

  • graph: 必须是一个矩阵且大小为nxn的形式
  • n_clusters=4: 需要提取出的群集数
  • eigen_solver='arpack': 解特征值的方式

开一张新影像label_im用来展示spectral_clustering切开后的分类结果

  1. labels = spectral_clustering(graph, n_clusters=4, eigen_solver='arpack')
  2. label_im = -np.ones(mask.shape)
  3. label_im[mask] = labels
  4. plt.matshow(img)
  5. plt.matshow(label_im)

EX 12:Spectral clustering for image segmentation - 图1
EX 12:Spectral clustering for image segmentation - 图2

(三)完整程式码

Python source code:plot_segmentation_toy.py

http://scikit-learn.org/stable/_downloads/plot_segmentation_toy.py

  1. print(__doc__)
  2. # Authors: Emmanuelle Gouillart <emmanuelle.gouillart@normalesup.org>
  3. # Gael Varoquaux <gael.varoquaux@normalesup.org>
  4. # License: BSD 3 clause
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. from sklearn.feature_extraction import image
  8. from sklearn.cluster import spectral_clustering
  9. ###############################################################################
  10. l = 100
  11. x, y = np.indices((l, l))
  12. center1 = (28, 24)
  13. center2 = (40, 50)
  14. center3 = (67, 58)
  15. center4 = (24, 70)
  16. radius1, radius2, radius3, radius4 = 16, 14, 15, 14
  17. circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1 ** 2
  18. circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2 ** 2
  19. circle3 = (x - center3[0]) ** 2 + (y - center3[1]) ** 2 < radius3 ** 2
  20. circle4 = (x - center4[0]) ** 2 + (y - center4[1]) ** 2 < radius4 ** 2
  21. ###############################################################################
  22. # 4 circles
  23. img = circle1 + circle2 + circle3 + circle4
  24. mask = img.astype(bool)
  25. img = img.astype(float)
  26. img += 1 + 0.2 * np.random.randn(*img.shape)
  27. # Convert the image into a graph with the value of the gradient on the
  28. # edges.
  29. graph = image.img_to_graph(img, mask=mask)
  30. # Take a decreasing function of the gradient: we take it weakly
  31. # dependent from the gradient the segmentation is close to a voronoi
  32. graph.data = np.exp(-graph.data / graph.data.std())
  33. # Force the solver to be arpack, since amg is numerically
  34. # unstable on this example
  35. labels = spectral_clustering(graph, n_clusters=4, eigen_solver='arpack')
  36. label_im = -np.ones(mask.shape)
  37. label_im[mask] = labels
  38. plt.matshow(img)
  39. plt.matshow(label_im)
  40. ###############################################################################
  41. # 2 circles
  42. img = circle1 + circle2
  43. mask = img.astype(bool)
  44. img = img.astype(float)
  45. img += 1 + 0.2 * np.random.randn(*img.shape)
  46. graph = image.img_to_graph(img, mask=mask)
  47. graph.data = np.exp(-graph.data / graph.data.std())
  48. labels = spectral_clustering(graph, n_clusters=2, eigen_solver='arpack')
  49. label_im = -np.ones(mask.shape)
  50. label_im[mask] = labels
  51. plt.matshow(img)
  52. plt.matshow(label_im)
  53. plt.show()