在(纯)PHP / MySQL中查找类似的图像 [英] Find similar images in (pure) PHP / MySQL

查看:153
本文介绍了在(纯)PHP / MySQL中查找类似的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户正在将图片上传到我的网站,我想先向他们提供已上传的图片。我的想法是
1.为每个现有图像创建某种图像哈希
2.创建新上传图像的哈希并将其与数据库中的其他图像进行比较

My users are uploading images to my website and i would like first to offer them already uploaded images first. My idea is to 1. create some kind of image "hash" of every existing image 2. create a hash of newly uploaded image and compare it with the other in the database

我找到了一些有趣的解决方案,如 http://www.pureftpd。 org / project / libpuzzle http://phash.org/ 等,但他们有一个或更多问题

i have found some interesting solutions like http://www.pureftpd.org/project/libpuzzle or or http://phash.org/ etc. but they got one or more problems


  1. 他们需要对PHP进行一些非标准扩展(或者根本不在PHP中) - 这对我来说没问题,但是我想将它作为我的流行CMS的插件创建,它在许多托管环境中使用而无法控制。

  2. 他们正在比较两个图像,但我需要比较一个到多个(例如成千上万)并且一个接一个地做它会非常无效/慢...
    ...

我会可以找到非常相似的图像(例如,不同的尺寸,重新保存的jpg或不同的jpg压缩因子)。

I would be OK to find only VERY similar images (so e.g. different size, resaved jpg or different jpg compression factor).

我得到的唯一想法是将图像大小调整为例如5px * 5px * 256种颜色,创建它的字符串表示然后找到相同的颜色。但我猜它可能会产生微小的颜色差异,即使只有两个不同大小的相同图像,所以找到100%相同的图像将是无用的。

The only idea I got is to resize the image to e.g. 5px*5px* 256 colors, create a string representation of it and then find the same. But I guess that it may have create tiny differences in colors even with just two same images with different size, so finding just the 100 % same would be useless.

所以我需要一些良好的图像字符串表示格式,这可能与某些SQL函数一起用于查找类似或其他一些好方法。例如。 phash创建感知哈希,所以当两个数字接近时,图像也应该接近,所以我只需找到最近的距离。但它又是外部库。

So I would need some good format of that string representation of image which than could be used with some SQL function to find similar, or some other nice way. E.g. phash create perceptional hashes, so when two numbers are close, the images should be close as well, so i just need to find closest distances. But it is again external library.

有没有简单的方法?

推荐答案

之前我遇到过这个完全相同的问题。

I've had this exact same issue before.

随意复制我所做的事情,希望它能帮助你/解决你的问题。

Feel free to copy what I did, and hopefully it will help you / solve your problem.

我的第一个想法是失败,类似于你的想法,我最终为每一张图片(无论大小)。但我很快就解决了这个超快速填充数据库的问题,并且无效。

My first idea that failed, similar to what you may be thinking, is I ended up making strings for every single image (no matter what size). But I quickly worked out this fills your database super fast, and wasn't effective.

下一个选项(可行)是一个较小的图像(如 5px idea),我的确如此,但是 10px * 10px 图片。我为每个图像创建'hash'的方式是 imagecolorat()函数。

Next option (that works) was a smaller image (like your 5px idea), and I did exactly that, but with 10px*10px images. The way I created the 'hash' for each image was the imagecolorat() function.

在这里查看php.net。

收到 rgb 图像的颜色,我将它们四舍五入到最近的 50 ,以便颜色不那么具体。该数字( 50 )是您想要更改的内容,具体取决于您希望搜索的具体

When receiving the rgb colours for the image, I rounded them to the nearest 50, so that the colours were less specific. That number (50) is what you want to change depending on how specific you want your searches to be.

例如:

// Pixel RGB
rgb(105, 126, 225) // Original
rgb(100, 150, 250) // After rounding numbers to nearest 50

对每个像素执行此操作后( 10px * 10px 将为您提供100 rgb()返回),然后我将它们变成一个数组,然后将它们存储在数据库中 base64_encode() serialize()

After doing this to every pixel (10px*10px will give you 100 rgb()'s back), I then turned them into an array, and stored them in the database as base64_encode() and serialize().

在搜索相似的图像时,我做了完全相同的过程到他们想要上传的图像,然后从数据库中提取图像'哈希',将它们全部进行比较,看看匹配的圆形 rgb 是什么。

When doing the search for images that are similar, I did the exact same process to the image they wanted to upload, and then extracted image 'hashes' from the database to compare them all, and see what had matching rounded rgb's.


  • 更大 50 rgb 四舍五入,不太具体您的搜索结果(反之亦然)。

  • The Bigger that 50 is in the rgb rounding, the less specific your search will be (and vice versa).

如果您希望 SQL 更具体,那么最好在数据库中存储有关图像的额外/特定信息,这样您就可以限制搜索结果在数据库中。的例如即可。如果宽高比 4:3 ,则只从数据库中提取 4:3 周围的图像。 (等)

If you want your SQL to be more specific, it may be better to store extra/specific info about the image in the database, so that you can limit the searches you get in the database. eg. if the aspect ratio is 4:3, only pull images around 4:3 from the database. (etc)

很难完美地解决这个问题 5px * 5px ,所以建议是 phpthumb 。我用它的语法:

It can be difficult to get this perfectly 5px*5px, so a suggestion is phpthumb. I used it with the syntax:


phpthumb.php?src=IMAGE_NAME_HERE.png&w=10&h=10&zc=1
// &w=  width of your image
// &h=  height of your image
// &zc= zoom control. 0:Keep aspect ratio, 1:Change to suit your width+height




<祝你好运,希望我能提供帮助。

Good luck mate, hope I could help.

这篇关于在(纯)PHP / MySQL中查找类似的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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