0

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 -> Checking images for similarity with OpenCV However, I do not believe doing a histogram comparison will work here because the values are primarily black and white.

How would you approach a problem like this?

Original

Different

B-L
  • 144
  • 1
  • 8
  • What is your definition of *`Image Difference Score`*? – PM 77-1 Nov 23 '21 at 19:34
  • Hi, I would define it with the following example: I have two images of equal size, both white (so identical). After comparison the difference score would be 0%. If I color the bottom half of one of the images black, then and compare again, the difference score would be 50%. The image with the black bottom half is now 50% different than the solid white image. – B-L Nov 23 '21 at 19:39
  • Have a look: https://stackoverflow.com/questions/2603713/comparing-similar-images-with-python-pil – PM 77-1 Nov 23 '21 at 20:44
  • @PM77-1 thank you, i will take a look – B-L Nov 23 '21 at 21:04
  • Does this answer your question? [Comparing (similar) images with Python/PIL](https://stackoverflow.com/questions/2603713/comparing-similar-images-with-python-pil) – Vito Gentile Nov 25 '21 at 14:25

1 Answers1

1

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

B-L
  • 144
  • 1
  • 8