I have two images(small and big). One of them contains another one. Something like one image is a photo and another one is a picture of the page of the photoalbum where this photo is situated. I hope you understood what I said.
So how do I get coordinates (x,y) of a small image on the big one using PHP?
It is quite easy to do on your own, without relying on external libs other than
gd
.What you need to be aware of, is that you most likely cannot do a simple pixel per pixel check, as filtering and compression might slightly modify the value of each pixel.
The code I am proposing here will most likely be slow, if performance is a concern, you could optimize it or take shortcuts. Hopefully, the code puts you on the right track!
First, lets iterate on our pictures
The goal here is to find how similar the pixels are, and if they are somewhat similar, keep the potential position. Here, I iterate each and every pixel of the large picture, this could be a point of optimization.
Now, where does this error come from?
Getting the likeliness
What I did here is iterate over the small picture and a "window" in the large picture checking how much difference there was on the
red
,green
andblue
channel:So far, we've established a potential error value for every "window" in the large picture that could contain the small picture, and store them in an array if they seem "good enough".
Sorting
Now, we simply need to sort our best matches and keep the best one, it is most likely where our small picture is located:
Example
Given the two following pictures:
and
You should have the following result:
Which corresponds exactly with the position of our duck. The 4 other positions are 1 pixel up, down, left and right.
I am going to edit this entry after I'm done working on some optimizations for you, as this code is way too slow for big images (PHP performed even worse than I expected).
Edit
First, before doing anything to "optimize" the code, we need numbers, so I added
and
at the end to have a specific idea of how much time is taken during the algorithm. This way, I can know if my changes improve or make the code worse. Given that the current code with these pictures takes ~45 minutes to execute, we should be able to improve this time quite a lot.
A tentative that was not succesful, was to cache the
RGB
from the$needle
to try to accelerate theGetImageErrorAt
function, but it worsened the time.Given that our computation is on a geometric scale, the more pixels we explore, the longer it will take... so a solution is to skip many pixels to try to locate as fast as possible our picture, and then more accurately zone in on our position.
I modified the error function to take as a parameter how to increment the
x
andy
For example, passing
2
will make the function return 4 times faster, as we skip bothx
andy
values.I also added a
stepSize
for the main loop:Doing this, I was able to reduce the execution time from 2657 seconds to 7 seconds at a price of precision. I increased the
keepThreshold
to have more "potential results".Now that I wasn't checking each pixels, my best answer is:
As you can see, we're near our desired position, but it's not quite right.
What I'm going to do next is define a rectangle around this "pretty close" position to explore every pixel inside the
stepSize
we added.I'm now changing the lower part of the script for:
Which now gives me an output like:
Which is indeed the right answer, roughly 200 times faster than the initial script.
Edit 2
Now, my test case was a bit too simple... I changed it to a google image search:
Looking for this picture (it's located at
718,432
)Considering the bigger picture sizes, we can expect a longer processing time, but the algorithm did find the picture at the right position:
Edit 3
I decided to try the option I told you in the comment, to scale down the pictures before executing the find, and I had great results with it.
I added this code before the first loop:
And for them main loop I iterated on the resized assets with the resized width and height. Then, when adding to the array, I double the
x
andy
, giving the following:The rest of the code remains the same, as we want to do the precise location on the real size pictures. All you have to do is add at the end:
Using this version of the code, with the google image result, I had:
A 4 times performance increase!
Hope this helps
Use ImageMagick.
This page will give you answer: How can I detect / calculate if a small pictures is present inside a bigger picture?