『sklearn学习』沃德结构层次聚类的浣熊脸图像的演示
2016-12-04 22:41
393 查看
# 沃德结构层次聚类的浣熊脸图像的演示 # A demo of structured Ward hierarchical clustering on a raccoon face image import time as time import numpy as np import scipy as sp import matplotlib.pyplot as plt from sklearn.feature_extraction.image import grid_to_graph from sklearn.cluster import AgglomerativeClustering from sklearn.utils.testing import SkipTest from sklearn.utils.fixes import sp_version if sp_version < (0, 12): raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and" "thus does not include the scipy.misc.face() image.") # 创建数据 try: face = sp.face(gray=True) except AttributeError: # 较新版本的 scipy 的 face 函数放在 misc 中 # Newer versions of scipy have face in misc from scipy import misc face = misc.face(gray=True) # 调整到原始尺寸的 10%,以加快处理 # Resize it to 10% of the original size to speed up the processing face = sp.misc.imresize(face, 0.10) / 255.0 # 为什么要除以 255 X = np.reshape(face, (-1, 1)) # 定义数据的结构,连接到他们相邻的像素 # Define the structure A of the data, Pixels connected to their neighbors connectivity = grid_to_graph(*face.shape) # 计算聚类 print "Compute structured hierarchical clustering..." st = time.time() n_clusters = 15 # 区域的数量 # 此处传入的参数 connectivity 的作用是什么?不加入的话则运行时间要长得多,且图像效果也跟加入的差别很大 ward = AgglomerativeClustering(n_clusters=n_clusters, linkage="ward", connectivity=connectivity) ward.fit(X) label = np.reshape(ward.labels_, face.shape) print "Elapsed time: ", time.time() - st print "Number of pixels: ", label.size print "Number of clusters: ", np.unique(label).size # 将结果绘制在图上 # Plot the results on an image plt.figure(figsize=(5, 5)) plt.imshow(face, cmap=plt.cm.gray) for cluster_num in range(n_clusters): plt.contour(label == cluster_num, contours=1, colors=[plt.cm.spectral(cluster_num / float(n_clusters)), ]) plt.xticks(()) plt.yticks(()) plt.show()
相关文章推荐
- 使用层次聚类在结构点云中进行平面的快速提取
- CMD:[2]怎样获取文件夹的目录层次结构
- iOS 笔记四:视图坐标、层次结构、自定义视图、UIBezierPath等
- 基于总变差模型的纹理图像中图像主结构的提取方法。
- 聚类:层次聚类、基于划分的聚类(k-means)、基于密度的聚类、基于模型的聚类
- 演示ps高光在哪里以及如何分辨图像暗调、高光、中间调
- 层次聚类
- 关于添加引用维度时出现没有启用中间粒度属性层次结构的问题
- Java 异常类层次结构
- visual studio 小技巧 查看函数的调用层次结构 以及 部分常用快捷键
- 4.4 隐藏和禁用属性层次结构
- BMP图像结构及其大小计算
- iOS10 UI教程层次结构的事件
- android sdk api的层次结构
- 数据层次结构建模之二
- IOS学习笔记(五)之视图的层次结构,查找,常用属性与清理学习
- 存储器层次结构(1)
- IOS操作系统的层次结构
- TIF图像数据结构之解析
- 【Android】【API】Android SDK API 层次结构