How can I determine / calculate if there are small images inside the larger image?

What I'm trying to do in PHP using GD or ImageMagick is as follows:

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

Is it possible? How can this be achieved?

Thanks!!!!

+6
source share
3 answers

I quickly looked through PHP ImageMagick and GD , and none of them has a built-in way to do this. An approach might be to use ImageMagick to split a larger image into smaller ones (the same size as the smaller), and start comparing them to a smaller one.

However, it will be very slow, I suppose.

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

<?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 tested the above code in the Linux lineup with XAMPP for Linux 1.7.3a and ImageMagick 6.7.1-0 2011-07-10 Q16.

About Comparison I use the AE (Absolute Error) metric, which calculates how many pixels are different. The result is printed into the error stream (STERR). You can find more about image search in the form of imagemagick here .

Good luck :)

+4
source

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

Although I am sure that it can be used for PHP as well, because it is just a general algorithm, it is a pretty bad idea to use PHP to compare images.

Also refer to the Wikipedia Template for the article.

+1
source

OK If you have $ BigImage and $ SmallImage.

And you make the $ Final image into $ large 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 the $ smallimage base in the $ Final image, then "NO WAY" in PHP.

Try Java.

^ _ ^

0
source

Source: https://habr.com/ru/post/892680/


All Articles