.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cluster/plot_segmentation_toy.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 JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_cluster_plot_segmentation_toy.py: # =========================================== # التجميع الطيفي لتجزئة الصور # =========================================== في هذا المثال، يتم توليد صورة بدوائر متصلة ويتم استخدام التجميع الطيفي لفصل الدوائر. في هذه الإعدادات، يحل نهج :ref:`spectral_clustering` المشكلة المعروفة باسم "القطع الرسومية المعيارية": حيث يتم النظر إلى الصورة على أنها رسم بياني للبكسلات المتصلة، وتتمثل خوارزمية التجميع الطيفي في اختيار القطع الرسومية التي تحدد المناطق مع تقليل نسبة التدرج على طول القطع وحجم المنطقة. وبما أن الخوارزمية تحاول موازنة الحجم (أي موازنة أحجام المناطق)، إذا أخذنا دوائر بأحجام مختلفة، فإن التجزئة تفشل. بالإضافة إلى ذلك، نظرًا لعدم وجود معلومات مفيدة في شدة الصورة أو تدرجها، فإننا نختار إجراء التجميع الطيفي على رسم بياني يتم إعلامه بشكل ضعيف فقط بالتدرج. وهذا قريب من إجراء تقسيم فورونوي للرسم البياني. بالإضافة إلى ذلك، نستخدم قناع الأجسام لتقييد الرسم البياني إلى مخطط الأجسام. في هذا المثال، نحن مهتمون بفصل الأجسام عن بعضها البعض، وليس عن الخلفية. .. GENERATED FROM PYTHON SOURCE LINES 16-19 .. code-block:: Python # المؤلفون: مطوري سكايت-ليرن # معرف رخصة SPDX: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 20-22 توليد البيانات ----------------- .. GENERATED FROM PYTHON SOURCE LINES 22-42 .. code-block:: Python from sklearn.cluster import spectral_clustering import matplotlib.pyplot as plt from sklearn.feature_extraction import image import numpy as np l = 100 x, y = np.indices((l, l)) center1 = (28, 24) center2 = (40, 50) center3 = (67, 58) center4 = (24, 70) radius1, radius2, radius3, radius4 = 16, 14, 15, 14 circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1**2 circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2**2 circle3 = (x - center3[0]) ** 2 + (y - center3[1]) ** 2 < radius3**2 circle4 = (x - center4[0]) ** 2 + (y - center4[1]) ** 2 < radius4**2 .. GENERATED FROM PYTHON SOURCE LINES 43-45 رسم أربعة دوائر --------------------- .. GENERATED FROM PYTHON SOURCE LINES 45-54 .. code-block:: Python img = circle1 + circle2 + circle3 + circle4 # نستخدم قناعًا يحد من المقدمة: المشكلة التي نهتم بها هنا ليست فصل الأجسام عن الخلفية، # ولكن فصلها عن بعضها البعض. mask = img.astype(bool) img = img.astype(float) img += 1 + 0.2 * np.random.randn(*img.shape) .. GENERATED FROM PYTHON SOURCE LINES 55-57 تحويل الصورة إلى رسم بياني مع قيمة التدرج على الحواف. .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: Python graph = image.img_to_graph(img, mask=mask) .. GENERATED FROM PYTHON SOURCE LINES 61-63 خذ دالة متناقصة من التدرج مما يؤدي إلى تجزئة قريبة من تقسيم فورونوي .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python graph.data = np.exp(-graph.data / graph.data.std()) .. GENERATED FROM PYTHON SOURCE LINES 66-68 هنا نقوم بالتجميع الطيفي باستخدام محلح arpack لأن amg غير مستقر رقميا في هذا المثال. ثم نقوم برسم النتائج. .. GENERATED FROM PYTHON SOURCE LINES 68-80 .. code-block:: Python labels = spectral_clustering(graph, n_clusters=4, eigen_solver="arpack") label_im = np.full(mask.shape, -1.0) label_im[mask] = labels fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) axs[0].matshow(img) axs[1].matshow(label_im) plt.show() .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_segmentation_toy_001.png :alt: plot segmentation toy :srcset: /auto_examples/cluster/images/sphx_glr_plot_segmentation_toy_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-86 رسم دائرتين -------------------- هنا نكرر العملية أعلاه ولكن نأخذ في الاعتبار الدائرتين الأوليين فقط قمنا بتوليدهما. لاحظ أن هذا يؤدي إلى فصل أنظف بين الدوائر حيث يسهل موازنة أحجام المناطق في هذه الحالة. .. GENERATED FROM PYTHON SOURCE LINES 86-105 .. code-block:: Python img = circle1 + circle2 mask = img.astype(bool) img = img.astype(float) img += 1 + 0.2 * np.random.randn(*img.shape) graph = image.img_to_graph(img, mask=mask) graph.data = np.exp(-graph.data / graph.data.std()) labels = spectral_clustering(graph, n_clusters=2, eigen_solver="arpack") label_im = np.full(mask.shape, -1.0) label_im[mask] = labels fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) axs[0].matshow(img) axs[1].matshow(label_im) plt.show() .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_segmentation_toy_002.png :alt: plot segmentation toy :srcset: /auto_examples/cluster/images/sphx_glr_plot_segmentation_toy_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.803 seconds) .. _sphx_glr_download_auto_examples_cluster_plot_segmentation_toy.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_segmentation_toy.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/cluster/plot_segmentation_toy.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_segmentation_toy.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_segmentation_toy.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_segmentation_toy.zip ` .. include:: plot_segmentation_toy.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_