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

8月 21, 2021 online essay writer

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

Look at this article various other language

The Structural Similarity Index (SSIM) is just a perceptual metric that quantifies the image quality degradation this is certainly due to processing such as for instance information compression or by losses in information transmission. This metric is actually a complete reference that needs 2 images through the same shot, this implies 2 graphically identical images to your eye. The 2nd image generally speaking is compressed or has another type of quality, which will be the purpose of this index. SSIM is generally utilized in the video clip industry, but has too an application that is strong photography. SIM actually measures the difference that is perceptual two comparable pictures. It cannot judge which associated with the two is much better: that must definitely be inferred from once you understand which will be the initial one and which was subjected to extra processing such as for instance compression or filters.

In this essay, we shall show you how exactly to calculate accurately this index between 2 images making use of 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 crucial tools that may be utilized by the logic. This tools may be installed through PIP aided by the after command:

These tools are:

  • scikitimage: scikit-image is an accumulation of algorithms for image processing.
  • opencv: OpenCV is a extremely optimized collection with concentrate on real-time applications.
  • imutils: a number of convenience functions to produce image that is basic functions such as for example interpretation, rotation, resizing, skeletonization, displaying Matplotlib pictures, sorting contours, detecting sides, and many other things easier with OpenCV and both Python 2.7 and Python 3.

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

2. Write script

The logic to compare the pictures could be the following one. Utilising the compare_ssim approach to 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 Any dimensionality.

win_size: none or int

The side-length associated with the sliding screen found in comparison. Needs to be a value that is odd. If gaussian_weights does work, this really is ignored in addition to screen size shall rely on sigma.

gradientbool, optional

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

data_rangefloat, optional

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

multichannelbool, optional

If real, treat the final measurement for the array as networks. Similarity calculations are done individually for every channel then averaged.

gaussian_weightsbool, optional

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

fullbool, optional

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


The mean similarity that is structural the image.


The gradient for the similarity that is structural between X and Y [2]. This might be only came back if gradient is placed to real.


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

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

This script is dependent on the rule posted by @mostafaGwely with this repository at Github. The rule follows precisely the exact same logic declared in the repository, nevertheless it eliminates a mistake of printing the Thresh of the pictures. The output of operating the script because of the pictures using the command that is following

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

The algorithm will print a sequence particularly “SSIM: $value”, you could change it out while you want. The value of SSIM should be obviously 1.0 if you compare 2 exact images.


您的电子邮箱地址不会被公开。 必填项已用*标注