.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/color_exposure/plot_log_gamma.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_color_exposure_plot_log_gamma.py: ================================= Gamma and log contrast adjustment ================================= This example adjusts image contrast by performing a Gamma and a Logarithmic correction on the input image. .. GENERATED FROM PYTHON SOURCE LINES 10-84 .. image-sg:: /auto_examples/color_exposure/images/sphx_glr_plot_log_gamma_001.png :alt: Low contrast image, Gamma correction, Logarithmic correction :srcset: /auto_examples/color_exposure/images/sphx_glr_plot_log_gamma_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib import matplotlib.pyplot as plt import numpy as np from skimage import data, img_as_float from skimage import exposure matplotlib.rcParams['font.size'] = 8 def plot_img_and_hist(image, axes, bins=256): """Plot an image along with its histogram and cumulative histogram.""" image = img_as_float(image) ax_img, ax_hist = axes ax_cdf = ax_hist.twinx() # Display image ax_img.imshow(image, cmap=plt.cm.gray) ax_img.set_axis_off() # Display histogram ax_hist.hist(image.ravel(), bins=bins, histtype='step', color='black') ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0)) ax_hist.set_xlabel('Pixel intensity') ax_hist.set_xlim(0, 1) ax_hist.set_yticks([]) # Display cumulative distribution img_cdf, bins = exposure.cumulative_distribution(image, bins) ax_cdf.plot(bins, img_cdf, 'r') ax_cdf.set_yticks([]) return ax_img, ax_hist, ax_cdf # Load an example image img = data.moon() # Gamma gamma_corrected = exposure.adjust_gamma(img, 2) # Logarithmic logarithmic_corrected = exposure.adjust_log(img, 1) # Display results fig = plt.figure(figsize=(8, 5)) axes = np.zeros((2, 3), dtype=object) axes[0, 0] = plt.subplot(2, 3, 1) axes[0, 1] = plt.subplot(2, 3, 2, sharex=axes[0, 0], sharey=axes[0, 0]) axes[0, 2] = plt.subplot(2, 3, 3, sharex=axes[0, 0], sharey=axes[0, 0]) axes[1, 0] = plt.subplot(2, 3, 4) axes[1, 1] = plt.subplot(2, 3, 5) axes[1, 2] = plt.subplot(2, 3, 6) ax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0]) ax_img.set_title('Low contrast image') y_min, y_max = ax_hist.get_ylim() ax_hist.set_ylabel('Number of pixels') ax_hist.set_yticks(np.linspace(0, y_max, 5)) ax_img, ax_hist, ax_cdf = plot_img_and_hist(gamma_corrected, axes[:, 1]) ax_img.set_title('Gamma correction') ax_img, ax_hist, ax_cdf = plot_img_and_hist(logarithmic_corrected, axes[:, 2]) ax_img.set_title('Logarithmic correction') ax_cdf.set_ylabel('Fraction of total intensity') ax_cdf.set_yticks(np.linspace(0, 1, 5)) # prevent overlap of y-axis labels fig.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.854 seconds) .. _sphx_glr_download_auto_examples_color_exposure_plot_log_gamma.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-image/scikit-image/v0.23.2?filepath=notebooks/auto_examples/color_exposure/plot_log_gamma.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_log_gamma.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_log_gamma.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_