Home > Software engineering >  Python - Image Difference Score
Python - Image Difference Score

Time:11-26

What would be the pythonic way of scoring the differences between the two images below? (for example, quantify that the second image is 25% different than the first)

OpenCV seems to pop up a lot in discussions about image comparison such as in this discussion -> Original

Different

CodePudding user response:

I found an answer that worked for me so I wanted to share it in case anyone else has a similar question. The code compares the pixels of the two images and finds the differences. Identical pixels will have a value of 0. Knowing this, we can use numpy to find the non-zero pixels and use that number to calculate a difference score.

you can use this code

import cv2
import numpy as np

img1 = cv2.imread("16x16_orig.png", 0)

img2 = cv2.imread("16x16_dif.png", 0)

#--- take the absolute difference of the images ---
res = cv2.absdiff(img1, img2)
print(res)


#--- convert the result to integer type ---
res = res.astype(np.uint8)

print(np.count_nonzero(res))

#--- find percentage difference based on number of pixels that are not zero ---
percentage = (np.count_nonzero(res) * 100)/ res.size
print(percentage)

using the two images below will return a score of 50% which is what we expect.

Original Image Original Image

different Different Image

after we get the absdiff, the resulting array looks like this:

[[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207]
 [207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207]
 [207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207]
 [207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207]
 [207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207]
 [207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207]
 [207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207]
 [207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207]]

It is important to note that the images that you wish to compare will need to be the same size.

For those that are curious about the images in the original question. The result was a 2.56% difference with 1292 non zero pixels

  • Related