I am trying to compare two images and detect the difference between them whether in shape or color.

-The change in shape: the number of parts has changed ( increased or decreased). -The change in color: some of the parts were in pink and changed into white and vice versa.

I have tried 3 algorithms:

  1. Compare by Compare_ssim.
  2. Difference detection by PIL (ImageChops.difference).
  3. Images subtraction.

The first algorithm:

(score, diff) = compare_ssim(img1, img2, full=True)
diff = (diff * 255).astype("uint8")

The second algorithm:

from PIL import Image ,ImageChops
if diff.getbbox():

The third algorithm:

 image3= cv2.subtract(image1,image2)

The problem is these algorithms are so sensitive. If the images have different noises and different angle of capture, they consider that the two images are totally different. Any ideas to fix that?

image1 image2 result

  • $\begingroup$ Perhaps you can blur images with a gaussian kernel and quantize pixel values (instead of 256 color levels, say you quantize to 32 levels). That will round-off some noise. $\endgroup$
    – hazrmard
    Jan 31 '20 at 3:13
  • $\begingroup$ @hazrmard Could you explain how to quantize pixel values? $\endgroup$
    – fracv
    Jan 31 '20 at 10:28
  • $\begingroup$ Here's documentation for quantize for PIL. You choose the number of colors <256: pillow.readthedocs.io/en/3.1.x/reference/… $\endgroup$
    – hazrmard
    Jan 31 '20 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.