.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/image/spatial_correlation_coef.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_gallery_image_spatial_correlation_coef.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_image_spatial_correlation_coef.py:


Spatial Correlation Coefficient
===============================

The Spatial Correlation Coefficient can be applied to compare the spatial structure of two images, which can be valuable in various domains such as medical imaging, remote sensing, and quality assessment in manufacturing or design processes.

Let's consider a use case in medical imaging where Spatial Correlation Coefficient is used to compare the spatial correlation between a reference image and a reconstructed medical scan.
This can be particularly relevant in evaluating the accuracy of image reconstruction techniques or assessing the quality of medical imaging data.

.. GENERATED FROM PYTHON SOURCE LINES 12-13

Here's a hypothetical Python example demonstrating the usage of the Spatial Correlation Coefficient to compare two medical images:

.. GENERATED FROM PYTHON SOURCE LINES 13-22

.. code-block:: Python
   :lineno-start: 14


    import matplotlib.pyplot as plt
    import numpy as np
    import torch
    from skimage.data import shepp_logan_phantom
    from skimage.transform import iradon, radon, rescale

    from torchmetrics.image import SpatialCorrelationCoefficient








.. GENERATED FROM PYTHON SOURCE LINES 23-24

Create a Shepp-Logan phantom image

.. GENERATED FROM PYTHON SOURCE LINES 24-27

.. code-block:: Python
   :lineno-start: 24

    phantom = shepp_logan_phantom()
    phantom = rescale(phantom, scale=512 / 400)  # Rescaling to 512x512








.. GENERATED FROM PYTHON SOURCE LINES 28-29

Simulate projection data (sinogram) using Radon transform

.. GENERATED FROM PYTHON SOURCE LINES 29-32

.. code-block:: Python
   :lineno-start: 29

    theta = np.linspace(0.0, 180.0, max(phantom.shape), endpoint=False)
    sinogram = radon(phantom, theta=theta)








.. GENERATED FROM PYTHON SOURCE LINES 33-34

Perform reconstruction using the inverse Radon transform

.. GENERATED FROM PYTHON SOURCE LINES 34-36

.. code-block:: Python
   :lineno-start: 34

    reconstruction = iradon(sinogram, theta=theta, circle=True)








.. GENERATED FROM PYTHON SOURCE LINES 37-38

Display the results

.. GENERATED FROM PYTHON SOURCE LINES 38-47

.. code-block:: Python
   :lineno-start: 38

    fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 4))
    ax1.set_title("Original")
    ax1.imshow(phantom, cmap=plt.cm.Greys_r)
    ax2.set_title("Radon transform (Sinogram)")
    ax2.imshow(sinogram, cmap=plt.cm.Greys_r, extent=(0, 180, 0, sinogram.shape[0]), aspect="equal")
    ax3.set_title("Reconstruction from sinogram")
    ax3.imshow(reconstruction, cmap=plt.cm.Greys_r)
    fig.tight_layout()




.. image-sg:: /gallery/image/images/sphx_glr_spatial_correlation_coef_001.png
   :alt: Original, Radon transform (Sinogram), Reconstruction from sinogram
   :srcset: /gallery/image/images/sphx_glr_spatial_correlation_coef_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 48-49

Convert the images to PyTorch tensors

.. GENERATED FROM PYTHON SOURCE LINES 49-52

.. code-block:: Python
   :lineno-start: 49

    phantom_tensor = torch.from_numpy(phantom).float().unsqueeze(0).unsqueeze(0)
    reconstructed_tensor = torch.from_numpy(reconstruction).float().unsqueeze(0).unsqueeze(0)








.. GENERATED FROM PYTHON SOURCE LINES 53-54

Calculating the Spatial Correlation Coefficient

.. GENERATED FROM PYTHON SOURCE LINES 54-59

.. code-block:: Python
   :lineno-start: 54

    scc = SpatialCorrelationCoefficient()
    score = scc(preds=reconstructed_tensor, target=phantom_tensor)

    print(f"Spatial Correlation Coefficient between the images: {score}")
    fig.suptitle(f"Spatial Correlation Coefficient: {score:.5}", y=-0.01)




.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Spatial Correlation Coefficient between the images: 0.14591339230537415

    Text(0.5, -0.01, 'Spatial Correlation Coefficient: 0.14591')




.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_gallery_image_spatial_correlation_coef.py:

.. only:: html

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

    .. container:: sphx-glr-download sphx-glr-download-jupyter

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

    .. container:: sphx-glr-download sphx-glr-download-python

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: spatial_correlation_coef.zip <spatial_correlation_coef.zip>`


.. only:: html

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

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