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 really a perceptual metric that quantifies the image quality degradation that is due to processing such as for example data compression or by losings in information transmission. This metric is actually the full reference that will require 2 pictures through the exact exact same shot, this implies 2 graphically identical pictures to your eye. The 2nd image generally speaking is compressed or has a unique quality, which will be the purpose of this index. SSIM is generally found in the video clip industry, but has aswell an application that is strong photography. SIM really measures the perceptual distinction between two comparable pictures. It cannot judge which of this two is much better: that really must be inferred from once you understand that will be the one that is original that has been confronted with extra processing such as for example compression or filters.

In this specific article, we shall explain to you how exactly to compute this index between 2 pictures using Python.


To adhere to this guide you will require:

  • Python 3
  • PIP 3

With that said, allow’s get going !

1. Install Python dependencies

Before applying the logic, you will have to install some tools that are essential will undoubtedly be employed by the logic. This tools could be set up through PIP because of the following command:

These tools are:

  • scikitimage: scikit-image is an accumulation of algorithms for image processing.
  • opencv: OpenCV is just a extremely optimized library with concentrate on real-time applications.
  • imutils: a number of convenience functions to help make basic image processing functions such as for example interpretation, rotation, resizing, skeletonization, showing Matplotlib pictures, sorting contours, detecting sides, and even 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. Utilising the compare_ssim way of the measure module of Skimage. This technique computes the mean structural similarity index between two images. It gets as arguments:

X, Y: ndarray

Pictures of every dimensionality.

win_size: int or None

The side-length regarding the sliding screen found in comparison. Must certanly be an odd value. If gaussian_weights does work, it is ignored plus the screen size shall rely on sigma.

gradientbool, optional

If real, additionally return the gradient with regards to Y.

data_rangefloat, optional

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

multichannelbool, optional

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

gaussian_weightsbool, optional

If real, each area has its mean and variance spatially weighted by way of A gaussian kernel that is normalized of sigma=1.5.

fullbool, optional

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


The mean structural similarity over the image.


The gradient of this similarity that is structural between X and Y [2]. That is just came back if gradient essay writer is placed to real.


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

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

This script is dependent on the rule posted by @mostafaGwely about this repository at Github. The code follows precisely the exact same logic declared from the repository, nonetheless it eliminates a mistake of printing the Thresh of the pictures. The production of operating the script with all the pictures using the following command:

Will create the output that is followingthe command within the image makes use of the quick 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 worth of SSIM should really be clearly 1.0.

0 yorum

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir