我希望创建一个图像基表,然后将任何新图像与之进行比较,以确定新图像是否与基表完全相同(或接近)。

例如:如果你想减少100次相同图像的存储,你可以存储它的一个副本,并提供它的参考链接。当输入一个新图像时,你想要与现有的图像进行比较,以确保它不是重复的…想法吗?

我的一个想法是缩小到一个小缩略图,然后随机选择100个像素位置并进行比较。


当前回答

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年里出现了大量的论文)。 在其他答案中有一些提示。但这可能有点过头了,建议直方图方法就可以了。尽管我认为是多种不同的组合 快速的方法会更好。

其他回答

正如卡特曼所指出的,您可以使用任何类型的哈希值来查找精确的重复项。

寻找近距离图像的一个起点可能在这里。这是CG公司用来检查修改后的图像是否仍然显示本质上相同的场景的工具。

我认为值得在此基础上添加我构建的phash解决方案,我们已经使用了一段时间:Image:: phash。它是一个Perl模块,但主要部分是用c语言编写的。它比phash.org快几倍,并且为基于dct的phash提供了一些额外的特性。

我们已经在MySQL数据库上建立了数以千万计的图像索引,所以我想要一些快速的东西,也想要一种使用MySQL索引的方法(这与汉明距离不工作),这导致我使用“减少”哈希进行直接匹配,模块文档讨论了这一点。

使用起来很简单:

use Image::PHash;

my $iph1 = Image::PHash->new('file1.jpg');
my $p1   = $iph1->pHash();

my $iph2 = Image::PHash->new('file2.jpg');
my $p2   = $iph2->pHash();

my $diff = Image::PHash::diff($p1, $p2);

选择100个随机点可能意味着相似(有时甚至不相似)的图像将被标记为相同,我认为这不是您想要的。如果图像格式不同(png、jpeg等)、大小不同或元数据不同,MD5哈希就无法工作。将所有图像缩小到一个更小的尺寸是一个不错的选择,只要你使用的是一个好的图像库/快速的语言,做一个像素对像素的比较不应该花费太长时间,而且尺寸足够小。

你可以试着让它们变得很小,然后如果它们是一样的,就在更大的尺寸上进行另一次比较——这可能是速度和准确性的良好结合……

如果您有大量的图像,请查看Bloom过滤器,它使用多个散列来获得概率高但效率高的结果。如果图像的数量不是很大,那么像md5这样的加密散列应该足够了。

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.