Image comparison in php

空扰寡人 提交于 2019-12-19 09:27:58

问题


My scenario is as follows: I have to save 1000 of images in database, and then I have to compare new image with database images for matches (match should be 70% or more) to get the best match image from database in php.

is there any algorithm or method for fast comparison with better result ...

Thanks in advance :)


回答1:


Try this class. It support get hash string from image to store in database and compare with new image later:
https://github.com/nvthaovn/CompareImage

It is very fast and accurate, although not optimal code. I have 20000 pictures in my database.




回答2:


I would suggest you use a Perceptual Hash or similar - mainly for reasons of performance. In essence, you create a single number, or hash, for each image ONCE in your database at the point where you insert it, and retain that hash in the database. Then when you get a new image to insert, you calculate its hash and compare it to the PRE-CALCULATED hash of all the other images so that you don't have to drag all the megabytes of pixels of your existing images from disk to compare them.

The best pHASHes are scale-invariant and image format invariant. Here is an article by Dr Neal Krawetz... Perceptual Hashing.

ImageMagick can also do Perceptual Hashing and is callable from PHP - see here.




回答3:


This depends entirely on how smart you want the algorithm to be.

For instance, here are some issues:

cropped images vs. an uncropped image images with a text added vs. another without mirrored images The easiest and simplest algorithm I've seen for this is just to do the following steps to each image:

scale to something small, like 64x64 or 32x32, disregard aspect ratio, use a combining scaling algorithm instead of nearest pixel scale the color ranges so that the darkest is black and lightest is white rotate and flip the image so that the lighest color is top left, and then top-right is next darker, bottom-left is next darker (as far as possible of course) Edit A combining scaling algorithm is one that when scaling 10 pixels down to one will do it using a function that takes the color of all those 10 pixels and combines them into one. Can be done with algorithms like averaging, mean-value, or more complex ones like bicubic splines.

Then calculate the mean distance pixel-by-pixel between the two images.

To look up a possible match in a database, store the pixel colors as individual columns in the database, index a bunch of them (but not all, unless you use a very small image), and do a query that uses a range for each pixel value, ie. every image where the pixel in the small image is between -5 and +5 of the image you want to look up.

This is easy to implement, and fairly fast to run, but of course won't handle most advanced differences. For that you need much more advanced algorithms.



来源:https://stackoverflow.com/questions/34606736/image-comparison-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!