Finding spot on an image - mathematical way?

纵然是瞬间 提交于 2019-12-06 12:33:00

问题


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).

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


回答1:


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.



来源:https://stackoverflow.com/questions/15523104/finding-spot-on-an-image-mathematical-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!