如何定义两个图像是否相似?

时间:2019-05-17 14:41:18

标签: python python-3.x image-processing python-imaging-library similarity

我有包含图像的文件夹。一些图像具有重复或相似的图像(从另一个角度看同一场景的图像)或经过修改(图像的大小,模糊度或噪点过滤器有所不同)。我的任务是定义其中一些图像是否具有相似的图像

我找到了这段代码,但我不明白当其中一个图像被修改或从另一个角度看同一场景时,输出编号如何描述两个图像的相似性。

def compare(file1, file2):
    im = [None, None] # to hold two arrays
    for i, f in enumerate([file1, file2]):
        im[i] = (np.array(
Image.open('C:/Users/taras/Downloads/dev_dataset/dev_dataset/'+f+'.jpg')

                         .convert('L')            # convert to grayscale using PIL
                         .resize((32,32), resample=Image.BICUBIC)) # reduce size and smooth a bit using PIL
                 ).astype(np.int)   # convert from unsigned bytes to signed int using numpy
    return np.abs(im[0] - im[1]).sum() 

1 个答案:

答案 0 :(得分:1)

代码将图像转换为灰度并将其大小调整为32x32像素。这意味着所有细节都丢失了,无论原始图像的形状或大小如何,您都只能大致了解1024点的颜色/亮度。

然后它也对第二个图像执行此操作,然后每个图像具有1024个亮度。通过减法计算出每对亮度之间的绝对差异,然后将所有差异求和。

如果图像相同,则差异为零,结果较低。如果图像非常不同,则它们在每个区域中的亮度都将不同,并且将这些差异加起来会很大。

如果您喜欢谷歌搜索,就好像是“感知哈希”

这里是Bean先生和8x8灰色版本-将其视为64个数字的向量:

enter image description here enter image description here

以下是数字:

255 253 255 207 124 255 254 255 255 252 255 178  67 245 255 254 255 255 255 193 154 255 255 255 255 249 183 142 192 253 251 255 255 216  92 180 156 215 254 255 255 181  96 179 115 194 255 254 255 153  95 175  92 102 246 255 255 112  98 163  97  50 195 255

这里是帕丁顿(Paddington)和8x8灰色版本-现在他也只有64个数字:

enter image description here enter image description here

以下是数字:

247 244 166 123 114  65   0   2 223 235 163  65  30  48  20   0 218 197  59  61 110  37  32   0 140  67  14 149 183  65   7   2 57  25  64 175 169  69   0   2  51  29  57 131 112  31   3   0 60  63  59  38  14  51  32   0  59  87  61  13  11  53  46   0

那么数学很简单:

abs(255-247) + abs(253-244) + abs(255-166) ...
相关问题