I want to compare two images in PHP. If they are 95% or above similar then the result should be just displaying text Yes. If they are not similar the result should be No.
So
if ( )
{
echo "Yes";
}
else
{
echo "No";
}
Any way this is so far what I had found. http://pastebin.com/wgeu2DqE
I would like to give you where I found it but you would have to register.
Now I have this function to load image
function LoadPNG($imgname)
{
/* Attempt to open */
$im = @imagecreatefrompng($imgname);
/* See if it failed */
if(!$im)
{
/* Create a blank image */
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an error message */
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
I’ve been trying on my notepad++ and phpdesigner to come up with something but I get errors. I am not a php developer so I am asking a help from you guys. I just need comparison between two images and Yes or No. So this is pretty easy. I saw this too but couldn’t make sense out of it.
Thanks.
You can do the compare in several ways, depending on what kind of compare you want and the use case. “Similar” is a very ambiguous term.
There are image comparing libraries, or you can use the ImageMagick functions. There is even a binding for OpenCV to use with PHP.
Of course, the more complex the approach, the more flexible and better the results.
If you are comparing two images that ought to be equal and aren’t (for example, say, two surveillance camera frames), you can use the DIY method given below.
If there are distortions in hue and luminosity (e.g. noise), then the DIY method will not work, but ImageMagick will give good results.
Finally if you’re trying to map two views of the same object, e.g. from a camera that panned a foot to the left, or you are looking for an image “inside” another, etc., then OpenCV is the only viable alternative of the three.
A do-it-yourself approach, for the simplest case, could be to first check the aspect ratio of the two images, then reduce both to the same size (you can reduce them even further (say, scale 1:4) to filter out small differences and/or also reduce the work ahead).
At that point, you can compare the two images pixel by pixel using
getColorAt(and also optionally convert to grayscale to ignore differences in hue). The distance between two pixels can also be calculated in several ways (e.g. with CIE perceptual functions), or you can use the Euclidean distance – SQRT((r1-r2)*2 + (g1-g2)*2 + (b1-b2)**2).Distance can range from 0 to W*H*255*SQRT(3); you can map linearly these values to the range 0-100%.
This can tell you easily the difference between an image, and the same image with some lines drawn on it. If you distort the image (e.g. with a “pinch” filter), or rotate it a little, the resulting image will appear 95%, even 99% identical, and yet this algorithm could well report a very low match (for photo quality pictures, with continuous tones, match will still be good. For cartoons, it will be very poor).