【发布时间】:2017-06-09 13:05:59
【问题描述】:
在 python 2.7 中,我想比较 2 个图像,以便它返回相似百分比给我,怎么做?请一步一步地告诉我。谢谢!
【问题讨论】:
标签: image python-2.7 compare similarity
在 python 2.7 中,我想比较 2 个图像,以便它返回相似百分比给我,怎么做?请一步一步地告诉我。谢谢!
【问题讨论】:
标签: image python-2.7 compare similarity
在没有 openCV 和任何计算机视觉库的情况下,一种非常简单且快速的方法是通过以下方式对图片数组进行规范
import numpy as np
picture1 = np.random.rand(100,100)
picture2 = np.random.rand(100,100)
picture1_norm = picture1/np.sqrt(np.sum(picture1**2))
picture2_norm = picture2/np.sqrt(np.sum(picture2**2))
在定义了两个标准化图片(或矩阵)之后,您可以将要比较的图片相乘相加:
1)如果你比较相似的图片,总和将返回1:
In[1]: np.sum(picture1_norm**2)
Out[1]: 1.0
2) 如果它们不相似,您将得到一个介于 0 和 1 之间的值(乘以 100 后的百分比):
In[2]: np.sum(picture2_norm*picture1_norm)
Out[2]: 0.75389941124629822
请注意,如果您有彩色图片,则必须在所有 3 个维度上执行此操作,或者只比较灰度版本。我经常需要比较大量图片,这是一种非常快速的方法。
【讨论】:
你可以这样做:
#Dimension tuppel
dim = (100,100,3) #Image dim in y,x,channels
pic1 = np.random.rand(dim)
pic2 = np.random.rand(dim)
#Either use operations that can be performed on np arrays
#or use flatten to make your (100,100,3) Immage a 100*100*3 vector
#Do your computation with 3 channels
#reshape the image if flatten np.reshape(output,(dim))
DONE
【讨论】: