.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_advanced_image_processing_auto_examples_plot_histo_segmentation.py: Histogram segmentation ====================== This example does simple histogram analysis to perform segmentation. .. image:: /advanced/image_processing/auto_examples/images/sphx_glr_plot_histo_segmentation_001.png :class: sphx-glr-single-img .. code-block:: python import numpy as np from scipy import ndimage import matplotlib.pyplot as plt np.random.seed(1) n = 10 l = 256 im = np.zeros((l, l)) points = l*np.random.random((2, n**2)) im[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1 im = ndimage.gaussian_filter(im, sigma=l/(4.*n)) mask = (im > im.mean()).astype(np.float) mask += 0.1 * im img = mask + 0.2*np.random.randn(*mask.shape) hist, bin_edges = np.histogram(img, bins=60) bin_centers = 0.5*(bin_edges[:-1] + bin_edges[1:]) binary_img = img > 0.5 plt.figure(figsize=(11,4)) plt.subplot(131) plt.imshow(img) plt.axis('off') plt.subplot(132) plt.plot(bin_centers, hist, lw=2) plt.axvline(0.5, color='r', ls='--', lw=2) plt.text(0.57, 0.8, 'histogram', fontsize=20, transform = plt.gca().transAxes) plt.yticks([]) plt.subplot(133) plt.imshow(binary_img, cmap=plt.cm.gray, interpolation='nearest') plt.axis('off') plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0, right=1) plt.show() **Total running time of the script:** ( 0 minutes 0.046 seconds) .. _sphx_glr_download_advanced_image_processing_auto_examples_plot_histo_segmentation.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_histo_segmentation.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_histo_segmentation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_