我想比较两个图像块,如果它们完全相同,结果必须是1,如果它们匹配60%,则答案必须为0.6。使用相关系数在Python中的两幅图像之间的百分比差异
在Matlab中,我可以使用corr2
命令来做到这一点,但在Python中我找不到方法。我试过numpy.corrcoef
,但它返回一个矩阵,并且scipy.signal.correlate2d
返回相同。
这是我曾尝试:
import numpy as np
import matplotlib.pyplot as plt
from skimage.filter import threshold_otsu
import matplotlib.cm as cm
import Image
import scipy
from PIL import Image as im
fname = 'testi.jpg'
image = Image.open(fname).convert("L")
arr = np.asarray(image)
global_thresh = threshold_otsu(arr)
global_otsu = arr >= global_thresh
global_otsu = np.invert(global_otsu).astype(int)
a1 = global_otsu[80:150,1350:1350+160]
fname1 = 'testi2.jpg'
image1 = Image.open(fname1).convert("L")
arr1 = np.asarray(image1)
global_thresh1 = threshold_otsu(arr1)
global_otsu1 = arr1 >= global_thresh1
global_otsu1 = np.invert(global_otsu1).astype(int)
a2 = global_otsu1[80:150,1350:1350+160]
co = scipy.signal.correlate2d(a1,a2)
plt.gray()
plt.subplot(121)
plt.imshow(a1)
plt.subplot(122)
plt.imshow(a2)
plt.show()
,其结果是:
[[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
...,
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]]
这些都是我要比较的图像:
将是大小相同的图片吗? – EvilTak
是的,他们将是相同的大小。 –
@EvilTak图像的尺寸相同 –