ᴍᴇʜᴏᴠ
ᴍᴇʜᴏᴠ

Reputation: 5256

Finding spot on an image - mathematical way?

I need to find a predefined area on an image and replace it with the other image (by just embedding the latter). The predefined spot would be a variable size rectangle area filled with solid background of the previously agreed colour.

Any advise on how do I achieve this?

I guess I can find a first pixel of the specified colour by doing a loop and going through the image row by row, pixel by pixel, but I feel like that's not the most efficient solution. Since the spot is supposed to be rather big, I also thought about going across the picture (please see the attachment below).

finding spot

So I need help in defining those loops. I believe I'll have to use some mathematical functions for that.

For example, if the big picture was square, the diagonal loop (the yellow EF line) would use the simplified linear function y=x (y=1x+0), but it's not likely to really be square. So I'll have to use the extended full linear function y=kx+b where k will have something to do with the rectangle size (I thought k=height/width), and b will be just 0. So the loop will look like:

$k = 1080/1920;
for ( $x=1920; $x>0; $x-- ) {
    $y = $k*$x;
}

But thats the yellow one, and the most simple I guess. Now, how do I define the others? Please help. Thanks

Upvotes: 1

Views: 238

Answers (1)

wsdookadr
wsdookadr

Reputation: 2662

Get pieces of your sub-image you're searching and search it in the big image.

You can use ImageMagick's sub-image search:

compare -verbose -dissimilarity-threshold 0.1 -subimage-search subimage.jpg bigimage.jpg

Read more about this here , here and here.

It will either tell you "TooDissimilar" or it will tell you the x,y position of the subimage.

Upvotes: 3

Related Questions