我希望创建一个图像基表,然后将任何新图像与之进行比较,以确定新图像是否与基表完全相同(或接近)。
例如:如果你想减少100次相同图像的存储,你可以存储它的一个副本,并提供它的参考链接。当输入一个新图像时,你想要与现有的图像进行比较,以确保它不是重复的…想法吗?
我的一个想法是缩小到一个小缩略图,然后随机选择100个像素位置并进行比较。
我希望创建一个图像基表,然后将任何新图像与之进行比较,以确定新图像是否与基表完全相同(或接近)。
例如:如果你想减少100次相同图像的存储,你可以存储它的一个副本,并提供它的参考链接。当输入一个新图像时,你想要与现有的图像进行比较,以确保它不是重复的…想法吗?
我的一个想法是缩小到一个小缩略图,然后随机选择100个像素位置并进行比较。
当前回答
我的公司每个月有大约2400万张来自制造商的图片。我正在寻找一个快速的解决方案,以确保我们上传到我们的目录的图像是新的图像。
I want to say that I have searched the internet far and wide to attempt to find an ideal solution. I even developed my own edge detection algorithm. I have evaluated speed and accuracy of multiple models. My images, which have white backgrounds, work extremely well with phashing. Like redcalx said, I recommend phash or ahash. DO NOT use MD5 Hashing or anyother cryptographic hashes. Unless, you want only EXACT image matches. Any resizing or manipulation that occurs between images will yield a different hash.
对于phash/ahash,查看这个:imagehash
我想通过发布我的代码和准确性来扩展*redcalx的帖子。
工作内容:
from PIL import Image
from PIL import ImageFilter
import imagehash
img1=Image.open(r"C:\yourlocation")
img2=Image.open(r"C:\yourlocation")
if img1.width<img2.width:
img2=img2.resize((img1.width,img1.height))
else:
img1=img1.resize((img2.width,img2.height))
img1=img1.filter(ImageFilter.BoxBlur(radius=3))
img2=img2.filter(ImageFilter.BoxBlur(radius=3))
phashvalue=imagehash.phash(img1)-imagehash.phash(img2)
ahashvalue=imagehash.average_hash(img1)-imagehash.average_hash(img2)
totalaccuracy=phashvalue+ahashvalue
以下是我的一些结果:
item1 item2 totalsimilarity
desk1 desk1 3
desk1 phone1 22
chair1 desk1 17
phone1 chair1 34
希望这能有所帮助!
其他回答
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年里出现了大量的论文)。 在其他答案中有一些提示。但这可能有点过头了,建议直方图方法就可以了。尽管我认为是多种不同的组合 快速的方法会更好。
这篇文章是我解决方案的起点,这里有很多好主意,所以我想分享我的结果。主要的见解是,我已经找到了一种方法,通过利用phash的速度来解决基于关键点的图像匹配的缓慢问题。
对于一般的解决方案,最好采用几种策略。每种算法都最适合于某些类型的图像转换,您可以利用这一点。
最上面是最快的算法;底部最慢(虽然更准确)。如果在更快的级别上找到了一个很好的匹配,您可能会跳过慢的级别。
基于文件哈希(md5,sha1等)的精确副本 用于缩放图像的感知哈希(phash) 用于修改图像的基于特征的(SIFT)
我的phash治疗效果很好。该方法对缩放后的图像具有较好的精度。它不适用于(感知上)修改过的图像(裁剪、旋转、镜像等)。为了处理散列速度,我们必须使用磁盘缓存/数据库来维护干草堆的散列。
phash真正的好处是,一旦你建立了哈希数据库(对我来说大约是1000张图片/秒),搜索可以非常非常快,特别是当你可以把整个哈希数据库保存在内存中时。这是相当实用的,因为哈希只有8个字节。
例如,如果您有100万张图像,则需要100万64位哈希值(8 MB)的数组。在某些cpu上,这适用于L2/L3缓存!在实际使用中,我看到corei7的速度超过1千兆哈姆/秒,这只是CPU内存带宽的问题。一个10亿张图片的数据库在64位CPU(需要8GB内存)上是可行的,搜索不会超过1秒!
For modified/cropped images it would seem a transform-invariant feature/keypoint detector like SIFT is the way to go. SIFT will produce good keypoints that will detect crop/rotate/mirror etc. However the descriptor compare is very slow compared to hamming distance used by phash. This is a major limitation. There are a lot of compares to do, since there are maximum IxJxK descriptor compares to lookup one image (I=num haystack images, J=target keypoints per haystack image, K=target keypoints per needle image).
为了解决速度问题,我尝试在每个找到的关键点周围使用phash,使用特征大小/半径来确定子矩形。使此工作良好的技巧是增加/缩小半径以生成不同的子矩形水平(在针图像上)。通常情况下,第一个级别(未缩放)将匹配,但通常需要更多。我不是100%确定为什么这是有效的,但我可以想象它可以实现对phash来说太小的功能(phash将图像缩小到32x32)。
另一个问题是SIFT不能最优地分配关键点。如果图像中有一个区域有很多边缘,那么关键点就会聚集在那里,而在其他区域则不会出现任何边缘。我在OpenCV中使用GridAdaptedFeatureDetector来改进分发。不知道什么网格大小是最好的,我使用一个小网格(1x3或3x1取决于图像方向)。
你可能想要在特征检测之前将所有的草堆图像(和针)缩放到更小的尺寸(我在最大尺寸上使用210px)。这将减少图像中的噪声(一直是计算机视觉算法的一个问题),也将探测器聚焦在更突出的特征上。
对于人物图像,您可以尝试面部检测并使用它来确定要缩放的图像大小和网格大小(例如最大的人脸缩放为100px)。特征检测器考虑多个等级(使用金字塔),但它将使用多少等级是有限制的(当然这是可调的)。
关键点检测器可能在返回的特性数量少于您想要的特性数量时工作得最好。例如,如果你要求400,得到300,那很好。如果你每次都能拿回400块,那么一些好的功能就会被忽略掉。
针状图像可以比干草堆图像拥有更少的关键点,但仍然可以获得良好的结果。增加更多并不一定能让你获得巨大的收益,例如当J=400和K=40时,我的命中率约为92%。当J=400和K=400时,命中率只能上升到96%。
我们可以利用汉明函数的极快速度来解决缩放、旋转、镜像等问题。可以使用多通道技术。在每次迭代中,转换子矩形,重新散列,并再次运行搜索函数。
如果您有大量的图像,请查看Bloom过滤器,它使用多个散列来获得概率高但效率高的结果。如果图像的数量不是很大,那么像md5这样的加密散列应该足够了。
我的公司每个月有大约2400万张来自制造商的图片。我正在寻找一个快速的解决方案,以确保我们上传到我们的目录的图像是新的图像。
I want to say that I have searched the internet far and wide to attempt to find an ideal solution. I even developed my own edge detection algorithm. I have evaluated speed and accuracy of multiple models. My images, which have white backgrounds, work extremely well with phashing. Like redcalx said, I recommend phash or ahash. DO NOT use MD5 Hashing or anyother cryptographic hashes. Unless, you want only EXACT image matches. Any resizing or manipulation that occurs between images will yield a different hash.
对于phash/ahash,查看这个:imagehash
我想通过发布我的代码和准确性来扩展*redcalx的帖子。
工作内容:
from PIL import Image
from PIL import ImageFilter
import imagehash
img1=Image.open(r"C:\yourlocation")
img2=Image.open(r"C:\yourlocation")
if img1.width<img2.width:
img2=img2.resize((img1.width,img1.height))
else:
img1=img1.resize((img2.width,img2.height))
img1=img1.filter(ImageFilter.BoxBlur(radius=3))
img2=img2.filter(ImageFilter.BoxBlur(radius=3))
phashvalue=imagehash.phash(img1)-imagehash.phash(img2)
ahashvalue=imagehash.average_hash(img1)-imagehash.average_hash(img2)
totalaccuracy=phashvalue+ahashvalue
以下是我的一些结果:
item1 item2 totalsimilarity
desk1 desk1 3
desk1 phone1 22
chair1 desk1 17
phone1 chair1 34
希望这能有所帮助!
我认为值得在此基础上添加我构建的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);