Compare 2 images and find % difference [closed]

2019-09-02 02:06发布

问题:

I want to compare two images and know the % difference between them. I am using raspbian on raspberry pi and python language. I have found PIL and magickimage, but with magick image I can't find a function for this and with PIL I have strange results.

For Pil i use this code :

h1 = image1.histogram()
h2 = image2.histogram()
rms = math.sqrt(reduce(operator.add,map(lambda a,b: (a-b)**2, h1, h2))/len(h1))

When i take two pics ( no difference ) with 0.5 seconds of intervall i have this results : rms = 4743.766.... If i move during between the two pics i have rms : 4699.288..... So it's does not make the difference between the two " sames " images and when i move :/

回答1:

Use compare which is part of ImageMagick. Like this:

compare -metric AE image1.png image2.png null:

The AE gives the absolute error, in terms of a count of the number of pixels difference. You can also use MAE (mean absolute error), or PAE (peak absolute error) or RMSE (root mean square error). You can also add a fuzz factor to allow slight differences in pixel values like this:

compare -fuzz 10% -metric AE image1.png image2.png null:

If you want the answer in a shell variable, say ndiff, you can do this:

ndiff=`compare -fuzz 10% -metric AE image1.png image2.png null: `
echo $ndiff