如何定义两个图像是否相似? [英] How can I define if two images are similar?

查看:175
本文介绍了如何定义两个图像是否相似?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have folder with images. Some images have duplicates or similar (images of the same scene from another angle) or modifications(images which differ by size, blur level or noise filters). My task is to define if some of these images have similar images

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

I find this code, but I can't understand how output number describes similarity of two images when of one of them is modified or the same scene from another angle.

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点的颜色/亮度.

The code converts the image to greyscale and resizes it to 32x32 pixels. That means all details are lost and you just get a general idea of the colour/brightness at 1024 points regardless of the shape or size of the original image.

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

It then does that for the second image too and it then has 1024 brightnesses for each image. It works out the absolute difference between each pair of brightnesses by subtraction and then totals all the differences up.

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

If the images are identical, the differences will be zero and the result will be low. If the images are very different, they will have different brightnesses in each area and adding up those differences will come to a large number.

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

It is like a "Perceptual Hash" if you feel like Googling.

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

Here is Mr Bean and an 8x8 grey version - think of it as a vector of 64 numbers:

以下是数字:

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个数字:

Here is Paddington and an 8x8 grey version - he too is now just 64 numbers:

以下是数字:

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) ...

这篇关于如何定义两个图像是否相似?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆