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

标签 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() 

最佳答案

该代码将图像转换为灰度并将其大小调整为 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

这是帕丁顿熊和 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) ...

关于python - 如何定义两个图像是否相似?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56188722/

相关文章:

python - 从图像中裁剪出互补区域

python - Django 用户 session 、Cookie 和超时

python - Cython: fatal error : 'numpy/arrayobject.h' 文件未找到,使用 numpy

python-3.x - Python3 的 bs4( BeautifulSoup )在 visual studio 中不起作用

image - Photoshop Action 对图像进行一定比例的填充

python - 在 python 中将图像划分为 5x5 block 并计算每个 block 的直方图

python - 修改 Python 矩阵算法中的列表

python - django安全模板过滤器定制

python-3.x - API VBA 的 eBay 数字签名可以,但 Python 签名验证无法满足请求

python-3.x - PyQt4 和 Python 3.5 的问题