我尝试在OpenCV Python中使用diff函数比较两个图像,但无法将一个图像与存储在测试文件夹中的图像进行比较。 那么如何使用OpenCV Python将一个输入图像与保存在文件夹中的测试图像进行比较?
答案 0 :(得分:1)
from PIL import Image
image1 = Image.open("image1.png")
image2 = Image.open("image2.png")
assert image1.size == image2.size, "Images are of Different sizes."
pairs = zip(image1.getdata(), image2.getdata())
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = image1.size[0] * image1.size[1] * 3
print("Difference (percentage):", (dif / 255.0 * 100) / ncomponents)
答案 1 :(得分:0)
此代码对我有用:
from PIL import Image
i1 = Image.open('./image_1.png')
for i in range(18):
i2 = Image.open('./image_%s.png'% i)
pairs = zip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
print ("Difference (percentage):", (dif / 255.0 * 100) / ncomponents)
它将比较文件夹中存在的所有名称为“ image_(int value)”的图像
输出:
Difference (percentage): 0.5852243327886709
Difference (percentage): 1.1135548577069718
Difference (percentage): 1.1761242170479302
.
.
.
.