{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Structural similarity index\n\nWhen comparing images, the mean squared error (MSE)--while simple to\nimplement--is not highly indicative of perceived similarity.  Structural\nsimilarity aims to address this shortcoming by taking texture into account\n[1]_, [2]_.\n\nThe example shows two modifications of the input image, each with the same MSE,\nbut with very different mean structural similarity indices.\n\n.. [1] Zhou Wang; Bovik, A.C.; ,\"Mean squared error: Love it or leave it? A new\n       look at Signal Fidelity Measures,\" Signal Processing Magazine, IEEE,\n       vol. 26, no. 1, pp. 98-117, Jan. 2009.\n\n.. [2] Z. Wang, A. C. Bovik, H. R. Sheikh and E. P. Simoncelli, \"Image quality\n       assessment: From error visibility to structural similarity,\" IEEE\n       Transactions on Image Processing, vol. 13, no. 4, pp. 600-612,\n       Apr. 2004.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom skimage import data, img_as_float\nfrom skimage.metrics import structural_similarity as ssim\nfrom skimage.metrics import mean_squared_error\n\n\nimg = img_as_float(data.camera())\nrows, cols = img.shape\n\nnoise = np.ones_like(img) * 0.2 * (img.max() - img.min())\nrng = np.random.default_rng()\nnoise[rng.random(size=noise.shape) > 0.5] *= -1\n\nimg_noise = img + noise\nimg_const = img + abs(noise)\n\nfig, axes = plt.subplots(nrows=1, ncols=3, figsize=(10, 4), sharex=True, sharey=True)\nax = axes.ravel()\n\nmse_none = mean_squared_error(img, img)\nssim_none = ssim(img, img, data_range=img.max() - img.min())\n\nmse_noise = mean_squared_error(img, img_noise)\nssim_noise = ssim(img, img_noise, data_range=img_noise.max() - img_noise.min())\n\nmse_const = mean_squared_error(img, img_const)\nssim_const = ssim(img, img_const, data_range=img_const.max() - img_const.min())\n\nax[0].imshow(img, cmap=plt.cm.gray, vmin=0, vmax=1)\nax[0].set_xlabel(f'MSE: {mse_none:.2f}, SSIM: {ssim_none:.2f}')\nax[0].set_title('Original image')\n\nax[1].imshow(img_noise, cmap=plt.cm.gray, vmin=0, vmax=1)\nax[1].set_xlabel(f'MSE: {mse_noise:.2f}, SSIM: {ssim_noise:.2f}')\nax[1].set_title('Image with noise')\n\nax[2].imshow(img_const, cmap=plt.cm.gray, vmin=0, vmax=1)\nax[2].set_xlabel(f'MSE: {mse_const:.2f}, SSIM: {ssim_const:.2f}')\nax[2].set_title('Image plus constant')\n\nplt.tight_layout()\nplt.show()"
      ]
    }
  ],
  "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.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}