【发布时间】:2016-04-18 04:32:59
【问题描述】:
我想比较两个图像块,如果它们完全相同,则结果必须为 1,如果它们匹配 60%,则答案必须为 0.6。
在 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 是的,因为 corr2 在 matlab 中返回
标签: matlab python-2.7 numpy image-processing scipy