{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Spatial Correlation Coefficient\n\nThe 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.\n\nLet'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.\nThis can be particularly relevant in evaluating the accuracy of image reconstruction techniques or assessing the quality of medical imaging data.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's a hypothetical Python example demonstrating the usage of the Spatial Correlation Coefficient to compare two medical images:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\nimport torch\nfrom skimage.data import shepp_logan_phantom\nfrom skimage.transform import iradon, radon, rescale\nfrom torchmetrics.image import SpatialCorrelationCoefficient" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a Shepp-Logan phantom image\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "phantom = shepp_logan_phantom()\nphantom = rescale(phantom, scale=512 / 400) # Rescaling to 512x512" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simulate projection data (sinogram) using Radon transform\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "theta = np.linspace(0.0, 180.0, max(phantom.shape), endpoint=False)\nsinogram = radon(phantom, theta=theta)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perform reconstruction using the inverse Radon transform\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reconstruction = iradon(sinogram, theta=theta, circle=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display the results\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 4))\nax1.set_title(\"Original\")\nax1.imshow(phantom, cmap=plt.cm.Greys_r)\nax2.set_title(\"Radon transform (Sinogram)\")\nax2.imshow(sinogram, cmap=plt.cm.Greys_r, extent=(0, 180, 0, sinogram.shape[0]), aspect=\"equal\")\nax3.set_title(\"Reconstruction from sinogram\")\nax3.imshow(reconstruction, cmap=plt.cm.Greys_r)\nfig.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert the images to PyTorch tensors\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "phantom_tensor = torch.from_numpy(phantom).float().unsqueeze(0).unsqueeze(0)\nreconstructed_tensor = torch.from_numpy(reconstruction).float().unsqueeze(0).unsqueeze(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculating the Spatial Correlation Coefficient\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scc = SpatialCorrelationCoefficient()\nscore = scc(preds=reconstructed_tensor, target=phantom_tensor)\n\nprint(f\"Spatial Correlation Coefficient between the images: {score}\")\nfig.suptitle(f\"Spatial Correlation Coefficient: {score:.5}\", y=-0.01)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" } }, "nbformat": 4, "nbformat_minor": 0 }