【问题标题】:Python 2.7 : compare similarity percentage between two images?Python 2.7:比较两个图像之间的相似度百分比?
【发布时间】:2017-06-09 13:05:59
【问题描述】:

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

【问题讨论】:

    标签: image python-2.7 compare similarity


    【解决方案1】:

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

    【讨论】:

    • 我如何定义图片以及如何使用 3 个维度来比较彩色图片?
    • 灰度图片在大多数情况下由m x n 数组表示,包含的值介于0 和255 或0 和1 之间。所以我的图片数组只是图片数据的示例。彩色图片通常在 m x n 数组中的每个项目处包含一个大小为 3 的数组。 3 个条目代表颜色 r,g,b。
    • 如果您不介意,请告诉我彩色图像的分步过程。我会感谢你的
    • 解决方案取决于您的原始数据的格式。您的数据看起来如何?顺便说一句:如果您想比较两张图片的内容,这不是正确的方法。在这种情况下,您应该使用 opencv。但是,如果您只是想比较像素或重叠,那就是正确的方法。
    • 不,我不想比较像素和重叠我想比较内容。基本上我有 2 个 URL,其中包含 2 个不同的图像,我只想比较它们。
    【解决方案2】:

    你可以这样做:

    #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
    

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 2011-08-09
      相关资源
      最近更新 更多