Just how to calculate the Structural Similarity Index (SSIM) between two images with Python

Check this out article in other language

The Structural Similarity Index (SSIM) is a perceptual metric that quantifies the image quality degradation this is certainly brought on by processing such as for example information compression or by losings in information transmission. This metric is simply a complete reference that will require 2 pictures through the exact exact same shot, this implies 2 graphically identical pictures to your human eye. The 2nd image generally is compressed or has a unique quality, which can be the purpose of this index. SSIM is generally utilized in the movie industry, but has also an application that is strong photography. SIM really steps the difference that is perceptual two comparable pictures. It cannot judge which of this two is much better: that needs to be inferred from once you understand that will be the one that is original which was confronted with extra processing such as for instance compression or filters.

In this specific article, we shall explain to you just how to calculate accurately this index between 2 images making use of 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, you will have to install some tools that are essential is likely to be utilized by the logic. This tools may be set up through PIP utilizing the command that is following

These tools are:

  • scikitimage: scikit-image is an accumulation of algorithms for image processing.
  • opencv: OpenCV is really a library that is highly optimized give attention to real-time applications.
  • imutils: a few convenience functions to help make basic image processing functions such as for instance interpretation, rotation, resizing, skeletonization, showing Matplotlib pictures, sorting contours, detecting sides, plus much more easier with OpenCV and both Python 2.7 and Python 3.

This guide will work with any platform where Python works (Ubuntu/Windows/Mac).

2. Write script

The logic to compare the pictures could be the after one. Making use of the compare_ssim way of the measure module of Skimage. This technique computes the mean structural similarity index between two pictures. It gets as arguments:

X, Y: ndarray

Pictures of every dimensionality.

win_size: none or int

The side-length for the sliding screen found in comparison. Should be an odd value. If gaussian_weights does work, this can be ignored and also the window size will be determined by sigma.

gradientbool, optional

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

data_rangefloat, optional

The information array of the input image (distance between minimal and maximum feasible values). By standard, this can be approximated through the image data-type.

multichannelbool, optional

If real, treat the dimension that is last of array as networks. 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 with A gaussian kernel that is normalized of sigma=1.5.

fullbool, optional

If real, also get back the entire similarity image the best essay writing service that is structural.


The mean structural similarity over the image.


The gradient of this similarity that is structural between X and Y [2]. This can be just came back if gradient is placed to real.


The complete SSIM image. This might be only came back if complete is placed to real.

As first, we are going to browse the pictures with CV through the provided arguments therefore we’ll use a black and white filter (grayscale) and then we’ll apply the mentioned logic to those pictures. Create the following script specifically script.py and paste the after logic on the file:

This script is founded on the rule posted by @mostafaGwely about this repository at Github. The rule follows precisely the same logic declared regarding the repository, nevertheless it eliminates a mistake of printing the Thresh of the pictures. The production of operating the script with all the pictures using the command that is following

Will create the output that is followingthe demand when you look at the image utilizes the brief argument description -f as –first and -s as –second ):

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