## Monday, March 19, 2018

### SSIM to Measure Image Similarity

There are situations where we need to measure the similarity between two images. For example, when we have an original image and few other images with lower quality, we might need to identify when one is mostly similar to the original image. Bit-wise comparison of the image pixels is not applicable in this kind of scenarios. We need something more sophisticated. Structural similarity index (SSIM) is such a method. It provides a value between 1 and 0 when comparing two images. If two images are exactly similar the SSIM becomes 1. If they are totally different, SSIM becomes 0.

Following Python program implements SSIM to compare between to images. Since we need some extra python libraries for this code to run, we should install following libraries on Ubuntu Linux.

sudo apt install python-skimage

sudo apt install python-opencv

SSIM calculation code is as follows.

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19``` ```from skimage.measure import structural_similarity as ssim import cv2 print("Reading files...") first = cv2.imread("image1.png") second = cv2.imread("image2.png") print("Resizing files...") first = cv2.resize(first, (2576,1125)) second = cv2.resize(second, (2576,1125)) print("Converting files to grayscale...") first = cv2.cvtColor(first, cv2.COLOR_BGR2GRAY) second = cv2.cvtColor(second, cv2.COLOR_BGR2GRAY) print("Calculating SSIM value...") s = ssim(first, second) print("first vs second", s) ```

~*******************~