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

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

Look at this article in other language

The Structural Similarity Index (SSIM) is a perceptual metric that quantifies the image quality degradation that is due to processing such as for instance information compression or by losings in data transmission. This metric is actually a full reference that needs 2 images through the same shot, this implies 2 graphically identical pictures to your eye. The second image generally speaking is compressed or has a unique quality, that is the aim of this index. SSIM is generally utilized in the movie industry, but has aswell an application that is strong photography. SIM really steps the perceptual distinction between two similar pictures. It cannot judge which associated with two is way better: that must definitely be inferred from once you understand which will be the initial one and which includes been subjected to extra processing such as for example compression or filters.

In this essay, we will explain to you just how to calculate accurately this index between 2 pictures using Python.


To check out 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 employed by the logic. This tools are set up through PIP with all the after demand:

These tools are:

  • scikitimage: scikit-image is an accumulation of algorithms for image processing.
  • opencv: OpenCV is just a very optimized collection with concentrate on real-time applications.
  • imutils: a number of convenience functions to help make basic image processing functions such as for instance interpretation, rotation, resizing, skeletonization, showing Matplotlib pictures, sorting contours, detecting edges, plus much more easier with OpenCV and both Python 2.7 and Python 3.

This tutorial will focus on any platform where Python works (Ubuntu/Windows/Mac).

2. Write script

The logic to compare the pictures is the after one. Making use of the compare_ssim approach to the measure module of Skimage. This process computes the mean structural similarity index 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. Should be a value that is odd. If gaussian_weights holds true, this is certainly ignored and also the screen size shall rely on sigma.

gradientbool, optional

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

data_rangefloat, optional

The information number of the input image (distance between minimal and maximum feasible values). By standard, this is certainly calculated through the image data-type.

multichannelbool, optional

If True, treat the dimension that is last of array as stations. Similarity calculations are done individually for every channel then averaged.

gaussian_weightsbool, optional

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

fullbool, optional

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


The mean structural similarity over the image.


The gradient associated with similarity that is structural between X and Y [2]. It is just came back if gradient is defined to real.


The SSIM that is full image. This can be just came back if complete is placed to real.

As first, we are going to see the pictures with CV through the supplied arguments therefore we’ll use a black colored and filter that is whitegrayscale) and then we’ll apply the mentioned logic to those pictures. Produce the following script specifically script.py and paste the after logic on the file:

This script is dependant on the rule posted by @mostafaGwely about this repository at Github. The rule follows precisely the exact same logic declared in the repository, nonetheless it removes a mistake of printing the Thresh of the pictures. The production of operating the script because of the images using the command that is following

Will create the following production (the 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 out 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 *