7

What I am trying to accomplish in PHP with GD or ImageMagick is the following:

I have one large image (say 2000 x 2000 pixels).. I would like to check if a second, smaller image (say 50 x 50 pixels) appears somewhere inside the larger picture, and to what percentage the area is a match.. So, for example, there's a 95% match of the smaller image at that place in the larger image..

Is this possible? How could this be achieved?

Thanks!!!!

Roel Krottje
  • 71
  • 1
  • 2
  • Are you looking for any approach (ie: brute force) or are you looking for a smarter algorithmic approach? – MoarCodePlz Jul 13 '11 at 13:26
  • You want to find specific smaller image (for which you have bitmap data), or just any generic image? Is small image size known or not? – lxa Jul 13 '11 at 14:13
  • Basically I'm looking for a smarter algorithmic approach, if this is possible in PHP with image comparison... I know everything, including the image size, of the smaller image. – Roel Krottje Jul 14 '11 at 05:58
  • Is small image a simple cropping of large image? Are scaling, rotations, color shifts to be considered? – Magicianeer Jul 21 '11 at 04:30

3 Answers3

4

I had a quick look in PHP's ImageMagick and GD and neither has a built in way of doing that. An approach could be to use ImageMagick to divide the larger image to smaller ones(same size as the smaller one) and start comparing them to the smaller one.

However this will be very slow I suppose.

You can do that with imagemagick if you use a system call in your PHP code. I don't know if you want to try this out but here is how it can be done:

<?php
//set a bigger time out limit because comparison takes a while
set_time_limit ( 275 ) ;

//the bigger image
$bigimage = "big.bmp";
//the smaller image
$smallimage = "small.bmp";
//result image
$resimg = "/tmp/similarity";

//system call
$output = shell_exec("(compare -metric AE -subimage-search ".$bigimage." ".$smallimage." ".$resimg."  > /dev/null) 3>&1 1>&2 2>&3");

//result is something like "0 @ 251,263"
$res = explode("@",$output);
if($res[0]==0)
{
    echo "Perfect match<br/>";
    $res = explode(",",$res[1]);

    echo "width: ".$res[0];
    echo "<br/>";
    echo "height: ".$res[1];
} else {
    echo "Not match";
}

?>

I have tested the above code in a linux box with XAMPP for Linux 1.7.3a and ImageMagick 6.7.1-0 2011-07-10 Q16.

About the comparison I use the metric AE(Absolute Error) which counts how many pixels differ. The result is printed to the error stream(STERR). More about imagemagick's subimage search you can find here.

Good luck :)

Master_ex
  • 789
  • 6
  • 12
  • If size difference of images is high, this will end up with error "compare: images too dissimilar" – PeterM Aug 09 '16 at 15:51
1

Here is a good answer for C#: How to find one image inside of another?

While I'm pretty sure that it can be used for PHP too because it's just a generic algorithm it's quite bad idea to use PHP for image comparison.

Also refer to Wikipedia Template match article.

Community
  • 1
  • 1
WASD42
  • 2,352
  • 6
  • 27
  • 41
-1

OK. If you have $BigImage , and $SmallImage.

And You make $Final image base on $big and $small images. Then you can check it with this function: http://www.php.net/manual/en/function.getimagesize.php

If You only have $FinalImage, and you want to track $smallimage base in $Final image, then "NO WAY" on PHP.

Try Java.

^_^

Ken Le
  • 1,787
  • 2
  • 22
  • 34
  • Well, smallimage is already present in bigimage.. - or not - that's what I have to check dynamically.. Check if this image appears in this bigger image.. Check if that image appears in this bigger image, etc.. – Roel Krottje Jul 14 '11 at 06:00