.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_auto_examples_applications_plot_image_comparison.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_applications_plot_image_comparison.py:


=======================
Visual image comparison
=======================

Image comparison is particularly useful when performing image processing tasks
such as exposure manipulations, filtering, and restauration.

This example shows how to easily compare two images with various approaches.




.. code-block:: python

    import matplotlib.pyplot as plt
    from matplotlib.gridspec import GridSpec

    from skimage import data, transform, exposure
    from skimage.util import compare_images


    img1 = data.coins()
    img1_equalized = exposure.equalize_hist(img1)
    img2 = transform.rotate(img1, 5)


    comp_equalized = compare_images(img1, img1_equalized, method='checkerboard')
    diff_rotated = compare_images(img1, img2, method='diff')
    blend_rotated = compare_images(img1, img2, method='blend')





.. code-block:: pytb

    Traceback (most recent call last):
      File "/home/jni/projects/scikit-image/doc/examples/applications/plot_image_comparison.py", line 21, in <module>
        img2 = transform.rotate(img1, 5)
      File "/home/jni/projects/scikit-image/skimage/transform/_warps.py", line 387, in rotate
        mode=mode, cval=cval, clip=clip, preserve_range=preserve_range)
      File "/home/jni/projects/scikit-image/skimage/transform/_warps.py", line 854, in warp
        warped = _warp_fast[ctype](image, matrix,
    TypeError: 'builtin_function_or_method' object is not subscriptable




Checkerboard
============

The `checkerboard` method alternates tiles from the first and the second
images.



.. code-block:: python


    fig = plt.figure(figsize=(8, 9))

    gs = GridSpec(3, 2)
    ax0 = fig.add_subplot(gs[0, 0])
    ax1 = fig.add_subplot(gs[0, 1])
    ax2 = fig.add_subplot(gs[1:, :])

    ax0.imshow(img1, cmap='gray')
    ax0.set_title('Original')
    ax1.imshow(img1_equalized, cmap='gray')
    ax1.set_title('Equalized')
    ax2.imshow(comp_equalized, cmap='gray')
    ax2.set_title('Checkerboard comparison')
    for a in (ax0, ax1, ax2):
        a.axis('off')
    plt.tight_layout()
    plt.plot()


Diff
====

The `diff` method computes the absolute difference between the two images.



.. code-block:: python


    fig = plt.figure(figsize=(8, 9))

    gs = GridSpec(3, 2)
    ax0 = fig.add_subplot(gs[0, 0])
    ax1 = fig.add_subplot(gs[0, 1])
    ax2 = fig.add_subplot(gs[1:, :])

    ax0.imshow(img1, cmap='gray')
    ax0.set_title('Original')
    ax1.imshow(img1_equalized, cmap='gray')
    ax1.set_title('Rotated')
    ax2.imshow(diff_rotated, cmap='gray')
    ax2.set_title('Diff comparison')
    for a in (ax0, ax1, ax2):
        a.axis('off')
    plt.tight_layout()
    plt.plot()


Blend
=====

`blend` is the result of the average of the two images.



.. code-block:: python


    fig = plt.figure(figsize=(8, 9))

    gs = GridSpec(3, 2)
    ax0 = fig.add_subplot(gs[0, 0])
    ax1 = fig.add_subplot(gs[0, 1])
    ax2 = fig.add_subplot(gs[1:, :])

    ax0.imshow(img1, cmap='gray')
    ax0.set_title('Original')
    ax1.imshow(img1_equalized, cmap='gray')
    ax1.set_title('Rotated')
    ax2.imshow(blend_rotated, cmap='gray')
    ax2.set_title('Blend comparison')
    for a in (ax0, ax1, ax2):
        a.axis('off')
    plt.tight_layout()
    plt.plot()

**Total running time of the script:** ( 0 minutes  0.000 seconds)


.. _sphx_glr_download_auto_examples_applications_plot_image_comparison.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: plot_image_comparison.py <plot_image_comparison.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: plot_image_comparison.ipynb <plot_image_comparison.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
