比较2张图片并找出%差异 [英] Compare 2 images and find % difference
问题描述
我想比较两个图像并知道它们之间的百分比差异。我在树莓派和python语言上使用raspbian。我已经找到了PIL和magickimage,但是使用magick image我找不到它的功能,而使用PIL我却得到了奇怪的结果。
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.
对于Pil,我使用以下代码:
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))
当我用0.5秒的间隔拍摄两张照片(无差异)时,我得到以下结果:rms = 4743.766 ....如果我在两张照片之间移动,则有rms: 4699.288 .....
因此,当我移动两个图像时,这并没有区别:/
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 :/
推荐答案
使用 compare
,它是ImageMagick的一部分。像这样:
Use compare
which is part of ImageMagick. Like this:
compare -metric AE image1.png image2.png null:
AE
根据像素差的计数给出绝对误差。您还可以使用 MAE
(平均绝对错误)或 PAE
(峰值绝对错误)或 RMSE
(均方根误差)。您还可以添加模糊
因子,以允许像素值略有不同,如下所示:
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:
如果您想要答案在shell变量中,例如 ndiff
,则可以执行以下操作:
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
这篇关于比较2张图片并找出%差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!