comparing 2 images in PHP - php

I am aware that there is one more question like mine, but I just thought mine was after all a bit different.
I have to be able to establish if the images are very similar or entirely different...
have a look at the following two images:
The first image is a bit lighter than the second image. You can see that on black striped fish in the middle.
So, comparing the md5 hashes doesn't really help. Is there anyother clever way to do it?
thanks!

try that function
http://www.php.net/manual/en/function.imagick-compareimages.php
you will need to google for usage since the doc seems to be empty ...

I am not sure if it would help, but I think if you run the images through GD image processing, it would really help you there!

this way is useful
$img1 = md5(file_get_content($image1))
...
if($img1 == $img2){
..
}

Try this .Someone wrote open source code .
http://compareimages.nikhazy-dizajn.hu/
Compare Images PHP Class:
This PHP Class compares two images and returns a number representing how similar they are. It is capable to tell if two pictures are similar even if they have different sizes or aspect ratio. Smaller number means the images are more similar. Numbers more than 10 means they are most likely not the same image.

Related

Compare Image with a Photo of the Image

I've a Problem to solve, but couldn't find a solution.
I need to compare a original image with a photo of the same image, and the function should return true if the photos are equal or false if the photos are not equal.
The photo can also have another size as the original image, and also if the photo contains only a part of the original it should detect the original.
Can I use normal face detection library's or do you have a better solution to solve this problem?
Thanks
There are several ways you could approach this problem. If you are looking to see if images are EXACTLY the same. You could go after the file. Using an md5 comparison you can help determine to see if it's the exact same file. Now this won't work for ACTUALLY comparing them.
If you want to actually compare the contents of the pictures you have, I suggest taking a look at PHP's gd library.
After some googling around I found a nice blog entry here about comparing the similarity of images. It's a good read.
A good method to start off with when comparing photos with GD is making the images the same size. The size should be reasonable so I'd say somewhere around 16x16. You should then consider RGB values, shapes, etc.
Some other libraries I should point you to are libpuzzle and imagemagick. Both of which make it pretty easy for comparing images in PHP. The documentation is pretty bad though so it may require a lot more googling and actual testing. Good luck!

Find similar images in (pure) PHP / MySQL

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
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
they need some nonstandard extension to PHP (or are not in PHP at all) - it would be OK for me, but I would like to create it as a plugin to my popular CMS, which is used on many hosting environments without my control.
they are comparing two images but i need to compare one to many (e.g. thousands) and doing it one by one would be very uneffective / slow ...
...
I would be OK to find only VERY similar images (so e.g. different size, resaved jpg or different jpg compression factor).
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.
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.
Is there any easy way?
I've had this exact same issue before.
Feel free to copy what I did, and hopefully it will help you / solve your problem.
How I solved it
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.
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.
See php.net here.
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.
for example:
// Pixel RGB
rgb(105, 126, 225) // Original
rgb(100, 150, 250) // After rounding numbers to nearest 50
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().
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.
Tips
The Bigger that 50 is in the rgb rounding, the less specific your search will be (and vice versa).
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)
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.
For an easy php implementation check out: https://github.com/kennethrapp/phasher
However - I wonder if there is a native mySql function for "compare" (see php class above)
I scale down image to 8x8 then I convert RGB to 1-byte HSV so result hash is 172 bytes string.
HSVHSVHSVHSVHSVHSVHSVHSV... (from 8x8 block, 172 bytes long)
0fff0f3ffff4373f346fff00...
It's not 100% accurate (some duplicates aren't found) but it works nice and looks like there is no false positive results.
Putting it down in an academical way, what you are looking for is a similarity function which takes in two images and returns an indicator how far/similar the two images are. This indicator could easily be a decimal number ranging from -1 to 1 (far apart to very close). Once you have this function you can set an image as a reference and compare all the images against it. Then finding the similar images to one is as simple as finding the closest similarity factor to it which is done with a simple search over a double field within an RDBMS like MySQL.
Now all that remains is how to define the similarity function. To be honest this is problem specific. It depends on what you call similar. But covariance is usually a good starting point, it just needs your two images to be of the same size which I think is of no big deal. Yet you can find lots of other ideas searching for 'similarity measures between two images'.

Best Way to Dynamically Resize Pictures

This is kind of a vague question, but I'm looking for suggestions and perhaps suggestions from people that have had experience with this issue. I have a PHP array of different sized images of products that I want to display on my web page with four to a row. The issue is that because they are different sizes, I don't know how to display them without them all coming out very "off".
Below is an example of a situation where it just looks weird:
I don't know the best way to position these images so they don't look "weird". Again, I know this is kind of vague, but perhaps someone has had experience with this? How do other shopping sites deal with this issue?
Thanks
Have you considered the fantastic masonry jQuery plugin?
Edit: Also available without jQuery: http://vanilla-masonry.desandro.com/
If I may, I would suggest you take a look at our service that includes image transformations in the cloud (and delivery through fast CDN).
You can use gravity based cropping while maintaining aspect ratio or face detection based cropping for reaching your desired images. Some examples of generating a 100x100 JPG of an uploaded image named 'face_top':
http://res.cloudinary.com/demo/image/upload/w_100,h_100,c_fill,g_north/face_top.jpg
http://res.cloudinary.com/demo/image/upload/w_100,h_100,c_fill,g_south/face_top.jpg
http://res.cloudinary.com/demo/image/upload/w_100,h_100,c_fill,g_face/face_top.jpg
http://res.cloudinary.com/demo/image/upload/w_100,h_100,c_thumb,g_face/face_top.jpg
Many more transformation options available.
I had answered something similar in another question, where I went onto adding an example of how to resize the images to the fixed thumbnail dimension, with option to either keep the aspect ratio (and letter box) or crop content to all thumbs are consistant.
Please have a look at http://pastebin.com/STzAxrZA and note the $maximize parameter in image_resize_jpg()
I guess you need to decide if you're trying to change the HTML (and keep odd sized images) or change the way the thumbnail images are generated

PHP: Building Smooth Image Transitions on the Fly

I'm trying to figure out via PHP if I can do the following. Imagine I have a two images. The first is an image of a square (50px by 50px). The second image, is that exact same square, only offset by 5px to the left.
I'm wondering if there is a way to dynamically generate all the images in-between to make this a smooth looking image transition (ie in this case generate the other 4 images to make it look like it moved from left to right). This would be a pretty simple situation, however would be applied to more complex images with the exact same premises, essentially using two images to predict the mediums.
Thank you in advance for your help
php would only be a wrapper, think eval() and google it, like http://www.linuxfocus.org/English/September2001/article139.shtml
regards,
/t
I believe the best thing you could hope to do with PHP is to do a 'morph' animation, where you sample the pixels for the first image and the second image, then create a third image consisting of the average color values. Repeating this process you could create as many sub-frames as you would like, but the result would be a blurring / morphing of image A -> image B, and would only be perceived as an animation with the simplest of input images.
More complicated algorithms including edge-detection or hinted-shape tweening could be utilized and implemented, however PHP might not be the best choice for this.
You can dynamically create images with a combination of PHP and ImageMagick.
You can pass in each dimension to ImageMagick, and it will generate an image on the fly for you. Create an image for each pixel you offset, resulting (in this case) in four extra images you can use later for your animation.

Php Motion Detection

Is it possible to achieve motion detection using php?
I'm not a GD/Image library expert, but shouldn't it be possible to detect if there's a significant difference between two images and have that trigger a warning?
I've been working on this type of problem recently. I've managed to compare two images together at any specified level resolution, and gather information about where change is happening, if any. An example using my code.
// setup two images to work with
$i1 = new SimpleImage("./img/0392.jpg");
$i2 = new SimpleImage("./img/0393.jpg");
$state = new State(15, 8, $i1);
$state = $state->difference(new State(15, 8, $i2), rgbColorDistance);
$state->abs()->denoiseStdDev()->scale(10)->round(0);
// $box will hold an array (x,y,w,h) that indicates location of change
$box = $state->getBoundingBox($i1->getWidth(), $i1->getHeight());
Examples and explanation on my blogpost
View code and download from Github
It's still rudimentary but it's a start towards my larger goal of constantly reading motion from a stream of images updated from a webcam.
I think in theory you can do this, you can come up with a basic image comparison. However doing this in PHP is not the best option, if you wanted to do fast frame by frame comparisons you would need to look into a compiled language.
As a basic image detection method you can do the following,
Greyscale Images, Sample every x pixels and compare (Use a threshold value). Then calculate the change percentage, if it's over say 15% you can assume motion.
It's a crude and basic method but it soulds like it will work for your needs.
Found something:
http://phpmotiondetect.sourceforge.net/
But I did not tested it.
Sure, you can use something like imagecolorat to get the colors from pixels on 2 different images.
So yeah, you could do it just like in any other language I guess.

Categories