Python 2.7:比较两个图像之间的相似度百分比?

时间:2017-06-09 13:05:59

标签: image python-2.7 compare similarity

在python 2.7中,我想比较2图像,以便它返回相似百分比给我,怎么做?请一步一步地告诉我。谢谢!

2 个答案:

答案 0 :(得分:0)

在没有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个维度中执行此操作,或者只是比较灰度版本。我经常需要比较大量的图片,这是一个非常快速的方法。

答案 1 :(得分:0)

您可以执行以下操作:

#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
相关问题