我希望创建一个图像基表,然后将任何新图像与之进行比较,以确定新图像是否与基表完全相同(或接近)。
例如:如果你想减少100次相同图像的存储,你可以存储它的一个副本,并提供它的参考链接。当输入一个新图像时,你想要与现有的图像进行比较,以确保它不是重复的…想法吗?
我的一个想法是缩小到一个小缩略图,然后随机选择100个像素位置并进行比较。
我希望创建一个图像基表,然后将任何新图像与之进行比较,以确定新图像是否与基表完全相同(或接近)。
例如:如果你想减少100次相同图像的存储,你可以存储它的一个副本,并提供它的参考链接。当输入一个新图像时,你想要与现有的图像进行比较,以确保它不是重复的…想法吗?
我的一个想法是缩小到一个小缩略图,然后随机选择100个像素位置并进行比较。
当前回答
几年前,我用PHP编写了一个非常简单的图像比较解决方案。它为每张图像计算一个简单的哈希值,然后找出差值。它的工作非常好裁剪或裁剪与翻译版本的同一图像。
首先,我将图像调整为较小的尺寸,如24x24或36x36。然后我取每一列的像素,并找出这一列的平均R,G,B值。
在每一列都有自己的三个数字之后,我进行两次传递:第一次是奇数列,第二次是偶数列。第一次传递将所有处理过的cols相加,然后除以它们的数([1]+ [2]+ [5]+ [N-1] / (N/2))。第二步以另一种方式进行:([3]-[4]+[6]-[8]…(n /2))。
现在我有两个数。正如我在实验中发现的,第一个是主要的:如果它与另一张图像的值相差甚远,那么从人类的角度来看,它们根本就不相似。
So, the first one represents the average brightness of the image (again, you can pay most attention to green channel, then the red one, etc, but the default R->G->B order works just fine). The second number can be compared if the first two are very close, and it in fact represents the overall contrast of the image: if we have some black/white pattern or any contrast scene (lighted buildings in the city at night, for example) and if we are lucky, we will get huge numbers here if out positive members of sum are mostly bright, and negative ones are mostly dark, or vice versa. As I want my values to be always positive, I divide by 2 and shift by 127 here.
我在2017年用PHP写的代码,似乎我丢失了代码。但我还有截图:
同样的图片:
黑白版:
裁剪版:
另一张图片,翻译版:
和第四色域一样,但另一个场景:
我调整了差异阈值,因此结果非常好。但是正如你所看到的,这个简单的算法对于简单的场景翻译并没有什么好处。
另一方面,我可以注意到,修改可以从两张图像中每一张以75- 80%的比例制作裁剪副本,角落4个,角落和边缘中间8个,然后以同样的方式将裁剪的变体与另一张完整的图像进行比较;如果其中一个相似度得分明显更高,那么就使用它的值而不是默认值)。
其他回答
我所知道的最好的方法是使用感知哈希。似乎有一个很好的开源实现这样的散列可用:
http://phash.org/
其主要思想是,通过识别原始图像文件中的显著特征,并对这些特征进行哈希(而不是直接对图像数据进行哈希),将每张图像简化为一个小的哈希代码或“指纹”。这意味着,相比简单的方法,如将图像缩小到一个小的拇指指纹大小的图像,并比较拇指指纹,假阳性率大大降低。
Phash提供了几种类型的哈希,可用于图像、音频或视频。
I believe that dropping the size of the image down to an almost icon size, say 48x48, then converting to greyscale, then taking the difference between pixels, or Delta, should work well. Because we're comparing the change in pixel color, rather than the actual pixel color, it won't matter if the image is slightly lighter or darker. Large changes will matter since pixels getting too light/dark will be lost. You can apply this across one row, or as many as you like to increase the accuracy. At most you'd have 47x47=2,209 subtractions to make in order to form a comparable Key.
I have an idea, which can work and it most likely to be very fast. You can sub-sample an image to say 80x60 resolution or comparable, and convert it to grey scale (after subsampling it will be faster). Process both images you want to compare. Then run normalised sum of squared differences between two images (the query image and each from the db), or even better Normalised Cross Correlation, which gives response closer to 1, if both images are similar. Then if images are similar you can proceed to more sophisticated techniques to verify that it is the same images. Obviously this algorithm is linear in terms of number of images in your database so even though it is going to be very fast up to 10000 images per second on the modern hardware. If you need invariance to rotation, then a dominant gradient can be computed for this small image, and then the whole coordinate system can be rotated to canonical orientation, this though, will be slower. And no, there is no invariance to scale here.
如果你想要更一般的东西或使用大数据库(百万张图片),那么 你需要研究图像检索理论(在过去5年里出现了大量的论文)。 在其他答案中有一些提示。但这可能有点过头了,建议直方图方法就可以了。尽管我认为是多种不同的组合 快速的方法会更好。
选择100个随机点可能意味着相似(有时甚至不相似)的图像将被标记为相同,我认为这不是您想要的。如果图像格式不同(png、jpeg等)、大小不同或元数据不同,MD5哈希就无法工作。将所有图像缩小到一个更小的尺寸是一个不错的选择,只要你使用的是一个好的图像库/快速的语言,做一个像素对像素的比较不应该花费太长时间,而且尺寸足够小。
你可以试着让它们变得很小,然后如果它们是一样的,就在更大的尺寸上进行另一次比较——这可能是速度和准确性的良好结合……
正如卡特曼所指出的,您可以使用任何类型的哈希值来查找精确的重复项。
寻找近距离图像的一个起点可能在这里。这是CG公司用来检查修改后的图像是否仍然显示本质上相同的场景的工具。