Python 3.x 使用numpy阵列比较两个相似的PIL图像不起作用

Python 3.x 使用numpy阵列比较两个相似的PIL图像不起作用,python-3.x,numpy,ubuntu,python-imaging-library,png,Python 3.x,Numpy,Ubuntu,Python Imaging Library,Png,我试图比较两幅图像,看看它们彼此有多相似 图一: 图二: 我尝试了两种不同的方法来比较这些图像,但两种方法都说它们并不接近 首先,尝试比较: from PIL import Image, ImageChops t1 = Image.open('./t1.png').convert("RGB") t2 = Image.open('./t2.png').convert("RGB") diff = ImageChops.difference(t1, t2) i

我试图比较两幅图像,看看它们彼此有多相似

图一:

图二:

我尝试了两种不同的方法来比较这些图像,但两种方法都说它们并不接近

首先,尝试比较:

from PIL import Image, ImageChops
t1 = Image.open('./t1.png').convert("RGB")
t2 = Image.open('./t2.png').convert("RGB")
diff = ImageChops.difference(t1, t2)

if diff.getbbox():
    print("images are different") # This is the result each time
else:
    print("images are the same")
我想它们可能只差几个像素,所以我试着与numpy进行比较:

import numpy as np
np_t1 = np.array(t1)
np_t2 = np.array(t2)
print(np.mean(np_t1 == np_t2)) # This should return something around .90 or above but instead it returns 0
不知道我做错了什么。以下是我的代码链接:


非常感谢您的帮助

它们是不同的形状:

t1.shape
(81, 81, 3)
t2.shape
(81, 80, 3)

print(np.mean(t1[:,:-1,:] == t2))
0.9441358024691358

使用t1==t2时,由于数组的大小不同,因此返回单个False,而在t1[:,:-1,:]==t2中,由于数组的形状相同,因此返回具有相同形状的数组,并进行元素比较。

它们是不同的形状:

t1.shape
(81, 81, 3)
t2.shape
(81, 80, 3)

print(np.mean(t1[:,:-1,:] == t2))
0.9441358024691358
使用t1==t2时,由于数组的大小不同,因此返回单个False,而在t1[:,:-1,:]==t2中,由于数组的形状相同,因此返回具有相同形状的数组,并进行元素比较