在本地计算机上进行反向图像搜索(用于图像重复项)

Reverse Image search (for image duplicates) on local computer

我有一堆从 pdf 中提取的质量较差的照片。我认识的某个人在她的计算机上的某处有高质量的照片(Mac),但据我所知,很难找到它们。

我愿意

示例伪代码

for each image in poorQualityImages:
    search ./macComputer for a higherQualityImage of image
    copy higherQualityImage to ./higherQualityImages

我需要执行一次此操作。 我正在寻找 工具、github 存储库或库 ,它们可以执行此功能,而不是深入了解基于内容的图像检索。


a post on reddit 有人试图做类似的事情

imgdupes 是一个看起来几乎可以实现此目的的程序,但我不想删除重复项,我想将最高质量的副本复制到目标文件夹


更新

给我以前的图像处理教授发邮件,他给我发了这个

Off the top of my head, nothing out of the box.

No guaranteed solution here, but you can narrow the search space. You’d need a little program that outputs the MSE or SSIM similarity index between two images, and then write another program or shell script that scans the hard drive and computes the MSE between each image on the hard drive and each query image, then check the images with the top X percent similarity score.

Something like that. Still not maybe guaranteed to find everything you want. And if the low quality images are of different pixel dimensions than the high quality images, you’d have to do some image scaling to get the similarity index. If the poor quality images have different aspect ratios, that’s even worse.

So I think it’s not hard but not trivial either. The degree of difficulty is partly dependent on the nature of the corruption in the low quality images.


更新

Github project I wrote which achieves what I want

前提

我会把我的回答集中在图像处理部分,因为我相信实现细节,例如遍历文件系统不是问题的核心。此外,以下所有内容只是我的拙见,我相信有更好的方法来检索我不知道的图像。无论如何,我同意你的教授所说的,我会遵循同样的思路,所以我会分享一些关于你可能使用的相似性指数的想法。

回答

  • MSE 和 SSIM - 这是一个可能的解决方案,正如您的教授所建议的那样。由于我假设低质量图像的分辨率也与好的图像不同,请记住对好的图像进行下采样(而不是对坏图像进行上采样)。
  • 图像减法(1-范数距离) - 减去两个图像 -> 如果它们相等,你将得到一个黑色图像。如果它们略有不同,则可以将非黑色像素(或像素强度之和)用作相似性指标。这实际上是1范数距离。
  • 直方图距离 - 可以参考这篇论文:https://www.cse.huji.ac.il/~werman/Papers/ECCV2010.pdf. Comparing two images' histograms might be potentially robust for your task. Check out this question too: Comparing two histograms
  • 嵌入学习 - 正如我所见,您将 tensorflow、keras 或 pytorch 作为标签,让我们考虑深度学习。这篇论文来到了我的 mind: https://arxiv.org/pdf/1503.03832.pdf 思路是学习一个 从图像 space 映射到欧几里得 space - 即计算一个 图像的嵌入。在嵌入 hyperspace 中,图像是 点。本文通过最小化 三重损失。三元组损失意味着最大化距离 不同 classes 的图像之间并最小化之间的距离 相同的图像 class。您可以在数据集上训练相同的模型 像 ImageNet。您可以通过降低 图像质量,以使模型 "invariant" 图像质量的差异(例如下采样后跟 上采样、图像压缩、添加噪声等)。一旦你可以 计算嵌入,你可以计算欧几里得距离(作为 MSE 的替代品)。这 可能 比使用 MSE/SSIM 作为相似性索引更好。 FaceNet 的回购:https://github.com/timesler/facenet-pytorch. Another general purpose approach (not related to faces) which might help you: https://github.com/zegami/image-similarity-clustering
  • Siamese networks for predicting similarity score - 我指的是这篇关于人脸验证的论文:http://bmvc2018.org/contents/papers/0410.pdf。孪生网络将两张图像作为输入并输出 [0, 1] 中的值。我们可以将输出解释为两幅图像属于同一幅图像的概率 class。您可以训练此类模型以预测 1 为以下类型的图像对:(质量良好的图像,人为退化的图像)。要再次降低图像质量,您可以组合例如下采样后跟 上采样、图像压缩、添加噪声等。让模型为不同 classes 的图像对(例如不同的图像)预测 0。网络的输出可以用作相似性指数。

备注1

这些不同的方法也可以结合使用。它们都为您提供了相似性指标,因此您可以非常轻松地对结果进行平均。

备注2

如果你只需要做一次,那么你在实施和训练深度模型上付出的努力可能是不合理的。我不建议这样做。尽管如此,如果您找不到任何其他解决方案并且 Mac 确实充满了图像并且无法进行手动搜索,您可以考虑它。

What you are looking for is called image hashing . In this answer you will find a basic explanation of the concept, as well as a go-to github repo for plug-and-play application.

散列的基本概念

From the repo page:“我们开发了一种基于 Marr 小波的新图像哈希,它计算基于边缘信息的感知哈希,特别强调角落。已经表明,人类视觉系统使特殊使用某些视网膜细胞来区分类似角的刺激。相信这种角信息可以用来区分激发这种方法的数字图像。基本上,从小波获得的边缘信息被压缩成一个固定长度的散列 72字节。二进制量化允许哈希之间相对快速的汉明距离计算。下面的散点图显示了我们标准图像语料库的结果。第一个图显示了每个图像与其被攻击的对应物之间的距离(例如内部距离)。第二个plot 显示了完全不同的图像之间的间距。虽然散列不是为处理旋转图像而设计的,但请注意轻微的旋转通常仍然落在阈值范围,因此通常可以匹配为相同。然而,这个散列的真正优势是与我们的 mvp 树索引结构一起使用。由于它比 dct 哈希更具描述性(长度为 72 个字节,而 dct 哈希为 8 个字节),因此为图像查询检索到的错误匹配要少得多。 “

另一个 blogpost 深入阅读,带有应用示例。

可用代码和用法

可以找到一个 github 存储库 here。显然还有更多的东西可以找到。 导入包后,您可以使用它来生成和比较哈希值:

>>> from PIL import Image
>>> import imagehash
>>> hash = imagehash.average_hash(Image.open('test.png'))
>>> print(hash)
d879f8f89b1bbf
>>> otherhash = imagehash.average_hash(Image.open('other.bmp'))
>>> print(otherhash)
ffff3720200ffff
>>> print(hash == otherhash)
False
>>> print(hash - otherhash)
36

演示 script find_similar_images 也提到了 github,说明了如何在目录中查找相似图像。

如果您查看 imgdupes 的文档,您会看到有以下选项:

--dry-run

dry run (do not delete any files)

因此,如果您 运行 imgdupes--dry-run 您将获得所有重复图像的列表,但实际上不会删除任何内容。您应该能够处理该输出以根据需要移动图像。

试试similar image finder 我已经开发解决这个问题了。 那里有解释和 algorithm,因此您可以根据需要实施自己的版本。