.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cluster/plot_coin_ward_segmentation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_cluster_plot_coin_ward_segmentation.py: ====================================================================== 硬币图像的结构化Ward层次聚类演示 ====================================================================== 使用Ward层次聚类计算二维图像的分割。聚类在空间上受到约束,以确保每个分割区域都是一个整体。 .. GENERATED FROM PYTHON SOURCE LINES 9-13 .. code-block:: Python # 作者:scikit-learn 开发者 # SPDX-License-Identifier:BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 14-16 生成数据 ------------- .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python from skimage.data import coins orig_coins = coins() .. GENERATED FROM PYTHON SOURCE LINES 23-26 将其调整为原始大小的20%以加快处理速度 在缩小之前应用高斯滤波进行平滑处理 可以减少混叠伪影。 .. GENERATED FROM PYTHON SOURCE LINES 26-42 .. code-block:: Python import numpy as np from scipy.ndimage import gaussian_filter from skimage.transform import rescale smoothened_coins = gaussian_filter(orig_coins, sigma=2) rescaled_coins = rescale( smoothened_coins, 0.2, mode="reflect", anti_aliasing=False, ) X = np.reshape(rescaled_coins, (-1, 1)) .. GENERATED FROM PYTHON SOURCE LINES 43-47 定义数据结构 ---------------------------- 像素与其邻居相连。 .. GENERATED FROM PYTHON SOURCE LINES 47-52 .. code-block:: Python from sklearn.feature_extraction.image import grid_to_graph connectivity = grid_to_graph(*rescaled_coins.shape) .. GENERATED FROM PYTHON SOURCE LINES 53-55 计算聚类 ------------------ .. GENERATED FROM PYTHON SOURCE LINES 55-73 .. code-block:: Python import time as time from sklearn.cluster import AgglomerativeClustering print("Compute structured hierarchical clustering...") st = time.time() n_clusters = 27 # number of regions ward = AgglomerativeClustering( n_clusters=n_clusters, linkage="ward", connectivity=connectivity ) ward.fit(X) label = np.reshape(ward.labels_, rescaled_coins.shape) print(f"Elapsed time: {time.time() - st:.3f}s") print(f"Number of pixels: {label.size}") print(f"Number of clusters: {np.unique(label).size}") .. rst-class:: sphx-glr-script-out .. code-block:: none Compute structured hierarchical clustering... Elapsed time: 0.088s Number of pixels: 4697 Number of clusters: 27 .. GENERATED FROM PYTHON SOURCE LINES 74-78 在图像上绘制结果 ---------------------------- 凝聚聚类能够分割每个硬币,然而,由于分割在背景中找到了一个较大的区域,我们不得不使用比硬币数量更多的 ``n_cluster`` 。 .. GENERATED FROM PYTHON SOURCE LINES 78-92 .. code-block:: Python import matplotlib.pyplot as plt plt.figure(figsize=(5, 5)) plt.imshow(rescaled_coins, cmap=plt.cm.gray) for l in range(n_clusters): plt.contour( label == l, colors=[ plt.cm.nipy_spectral(l / float(n_clusters)), ], ) plt.axis("off") plt.show() .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_coin_ward_segmentation_001.png :alt: plot coin ward segmentation :srcset: /auto_examples/cluster/images/sphx_glr_plot_coin_ward_segmentation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.146 seconds) .. _sphx_glr_download_auto_examples_cluster_plot_coin_ward_segmentation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/cluster/plot_coin_ward_segmentation.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_coin_ward_segmentation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_coin_ward_segmentation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_coin_ward_segmentation.zip ` .. include:: plot_coin_ward_segmentation.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_