How exactly to calculate the Structural Similarity Index (SSIM) between two images with Python

How exactly to calculate the Structural Similarity Index (SSIM) between two images with Python

Check this out article various other language

The Structural Similarity Index (SSIM) is really a perceptual metric that quantifies the image quality degradation this is certainly due to processing such as for example data compression or by losings in information transmission. This metric is actually a full reference that will require 2 pictures through the exact exact exact same shot, what this means is 2 graphically identical pictures towards the eye that is human. The 2nd image generally speaking is compressed or has an alternate quality, which can be the purpose of this index. SSIM is generally found in the movie industry, but has too a strong application in photography. SIM really steps the difference that is perceptual two similar pictures. It cannot judge which associated with two is much better: that must definitely be inferred from once you understand which can be the help me write my paper initial one and which was subjected to extra processing such as for instance compression or filters.

In this specific article, we will demonstrate how exactly to compute this index between 2 pictures utilizing Python.


To adhere to this guide you shall require:

  • Python 3
  • PIP 3

With that said, why don’t we get going !

1. Install Python dependencies

Before applying the logic, it is important to install some tools that are essential are going to be employed by the logic. This tools may be set up through PIP because of the command that is following

These tools are:

  • scikitimage: scikit-image is an accumulation of algorithms for image processing.
  • opencv: OpenCV is just a library that is highly optimized give attention to real-time applications.
  • imutils: a number of convenience functions to create image that is basic functions such as for example interpretation, rotation, resizing, skeletonization, showing Matplotlib images, sorting contours, detecting sides, and a lot more easier with OpenCV and both Python 2.7 and Python 3.

This guide shall focus on any platform where Python works (Ubuntu/Windows/Mac).

2. Write script

The logic to compare the pictures would be the after one. Utilising the compare_ssim way of the measure module of Skimage. This technique computes the mean similarity that is structural between two pictures. It gets as arguments:

X, Y: ndarray

Pictures of every dimensionality.

win_size: int or None

The side-length for the sliding screen found in comparison. Must certanly be a value that is odd. If gaussian_weights does work, that is ignored therefore the window size shall rely on sigma.

gradientbool, optional

If real, additionally get back the gradient with regards to Y.

data_rangefloat, optional

The info array of the input image (distance between minimal and maximum feasible values). By standard, it is predicted through the image data-type.

multichannelbool, optional

If True, treat the final measurement associated with the array as stations. Similarity calculations are done separately for every single channel then averaged.

gaussian_weightsbool, optional

If real, each spot has its mean and variance spatially weighted by way of a normalized gaussian kernel of width sigma=1.5.

fullbool, optional

If True, additionally get back the total structural similarity image.


The mean structural similarity over the image.


The gradient associated with the structural similarity index between X and Y [2]. This might be just came back if gradient is placed to real.


The complete SSIM image. That is just came back if complete is defined to real.

As first, we’ll see the pictures with CV through the supplied arguments and then we’ll apply a black colored and white filter (grayscale) and now we’ll apply the mentioned logic to those pictures. Create the following script specifically and paste the logic that is following the file:

This script will be based upon the rule posted by @mostafaGwely about this repository at Github. The code follows precisely the logic that is same from the repository, nevertheless it eliminates a mistake of printing the Thresh of the pictures. The production of operating the script because of the pictures using the following command:

Will create the output that is followingthe demand into the image makes use of the brief argument description -f as –first and -s as –second ):

The algorithm will namely print a string “SSIM: $value”, you could change it while you want. In the event that you compare 2 precise pictures, the worthiness of SSIM should really be demonstrably 1.0.

Leave a comment

Your email address will not be published. Required fields are marked *