I tested each individual component of that manually and they all worked, but when put together it doesn't find the proper coordinates, just bogus ones. If someone could shed some light on this I would really appreciate it :)

Thank you,
Dave Z

mlseim

03-11-2010, 08:41 PM

So you're saying that is compares every pixel in the small image to every pixel
in the large image to figure out where all of the pixels match?

bacterozoid

03-11-2010, 08:53 PM

Dude, your algorithm needs some work. Instead of storing everything in a massive string like that and then using explode, try this:

Load up both your large and small images

Use a loop (two levels deep) to go through every pixel in your large image. If the pixel you are at in your large image matches the top left pixel of your small image, then step into a verify subroutine

The verify subroutine would then check the other pixels to see if they are a match. You could probably get away with checking a fairly low percentage of pixels - maybe check a grid with 10px in between each. This would depend on the desired size of your small image, but you should be able to get pretty good results (and much more efficiently)

bacterozoid

03-11-2010, 09:26 PM

Because I thought the problem to be interesting and because you already tried yourself, here's one way to do it:

// Loop over every pixel in the large image
for($cy = 0; $cy < $big_h; $cy++) { // This loops over vertically
for($cx = 0; $cx < $big_w; $cx++) { // This loops over horizontally
if(imagecolorat($big, $cx, $cy) == imagecolorat($small, 0, 0)) { // See if the current pixel in the large image matches the top left pixel in the small one
if(verify($cx, $cy, $big, $small)) { // If there is a potential match, verify if it is a full match
die("X=$cx, Y=$cy"); // If it is a full match, stop doing work and output the result
}
}
}
}
echo "Not Found"; // No match found

// Check and see if the given cx, cy position is a match
function verify($cx, $cy, &$big, &$small) { // The & passes by reference...basically just saves you memory
$small_w = imagesx($small);
$small_h = imagesy($small);

// Loop over every pixel in the large image
for($cy = 0; $cy < $big_h; $cy++) { // This loops over vertically
for($cx = 0; $cx < $big_w; $cx++) { // This loops over horizontally
if(imagecolorat($big, $cx, $cy) == imagecolorat($small, 0, 0)) { // See if the current pixel in the large image matches the top left pixel in the small one
if(verify($cx, $cy, $big, $small)) { // If there is a potential match, verify if it is a full match
die("X=$cx, Y=$cy"); // If it is a full match, stop doing work and output the result
}
}
}
}
echo "Not Found"; // No match found

// Check and see if the given cx, cy position is a match
function verify($cx, $cy, &$big, &$small) { // The & passes by reference...basically just saves you memory
$small_w = imagesx($small);
$small_h = imagesy($small);

That works beautifully, thank you :) The only part I don't understand is in your for statement in the verify function where you put +=

Dude, your algorithm needs some work. Instead of storing everything in a massive string like that and then using explode, try this:

Load up both your large and small images

Use a loop (two levels deep) to go through every pixel in your large image. If the pixel you are at in your large image matches the top left pixel of your small image, then step into a verify subroutine

The verify subroutine would then check the other pixels to see if they are a match. You could probably get away with checking a fairly low percentage of pixels - maybe check a grid with 10px in between each. This would depend on the desired size of your small image, but you should be able to get pretty good results (and much more efficiently)

Thanks, I never thought of doing it like that :D

mlseim

03-12-2010, 12:52 AM

This:
$sy += $threshhold;

Is shorthand for this:
$sy = $sy + $threshhold;

Macmee

03-12-2010, 01:04 AM

This:
$sy += $threshhold;

Is shorthand for this:
$sy = $sy + $threshhold;

Ooh ok. So when it's verifying it's actually moving 20 pixels across and 20 pixels down to continue each pixel check?

bacterozoid

03-12-2010, 03:18 AM

Yep. This way you're not checking every single pixel - your script will complete quicker and use less resources.

Skipping every 20px (at least on an image this size) should generally be good enough to detect a match, as the likliehood of that happening anywhere else in the image is pretty low. If you have problems, just decrease that threshold value.