Difference detection between two images

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
img1=Image.open("canny1.jpg")
img2=Image.open("canny2.jpg")
diff=ImageChops.difference(img1,img2)
if diff.getbbox():
diff.show()


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?

• 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. – hazrmard Jan 31 at 3:13
• @hazrmard Could you explain how to quantize pixel values? – fracv Jan 31 at 10:28
• Here's documentation for quantize for PIL. You choose the number of colors <256: pillow.readthedocs.io/en/3.1.x/reference/… – hazrmard Jan 31 at 22:15