Locate subimage

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Locate subimage

Post by snibgo »

Good spot. Thanks Fred.

Any conversion to gray will give similar results. It is only for comparison purposes.

If a binary threshold is wanted:

Code: Select all

convert sub_image_water.png -scale 1x1! -scale 1024x632! zurich.png -compose Difference -composite -negate -fuzz 5% -fill Black +opaque white zurich_water_3.png
Image
snibgo's IM pages: im.snibgo.com
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Locate subimage

Post by VanGog »

Snigo, Well done. I found it is needed to add one more mask which is a bit darker. Down there at the river in the bottom there are ship-travel paths which are darker.

Code: Select all

convert sub_image_water_1.png -scale 1x1! -scale 1024x632! map.png -compose Difference -composite -negate -fuzz 7% -fill Black +opaque white temp_mask_1.png
convert sub_image_water_2.png -scale 1x1! -scale 1024x632! map.png -compose Difference -composite -negate -fuzz 7% -fill Black +opaque white temp_mask_2.png
convert temp_mask_1.png temp_mask_2.png ^
        -compose Lighten -composite mask_water.png
I will try to do it using layers later. It is definetely much faster. But let's say, that I would like to find blue rectangle with color of the water. Is it possible to find blue rectangle inside water surface using compare? Compare is not able to search only inside the area of mask?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Locate subimage

Post by snibgo »

VanGog wrote:But let's say, that I would like to find blue rectangle with color of the water. Is it possible to find blue rectangle inside water surface using compare?
I don't understand. Can you show an example?
snibgo's IM pages: im.snibgo.com
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Locate subimage

Post by VanGog »

Sorry that was wrong assumption == non-sense. So another idea.

If the white mask of the water would contain empty places, would you perform compare (slow) or could you perform a search of rectangle inside the white area? Let's have this mask as a result of some search of water surface. Let's suppose that the water surface is light blue while the rectangles are dark blue so they are not in the mask. Well, I could use this mask, but it is not perfect because it contains gaps. I do not want to expand and contract because that is problematic. Is it possible to locate these black rectangles inside white areas - I mean not to search complete image but only the white part. Does compare.exe search complete image? I think it does. And that would be not effective.

Image

It is the similar thing like here with routes:
viewtopic.php?f=1&t=29791
Check the last post with these images:
https://1.bp.blogspot.com/-66QdLjN0SDc/ ... y_mask.gif
extend makes problematic result - you would see 1 route instead 2 routes:
https://3.bp.blogspot.com/-6RuP5XZ65gE/ ... old_40.png
or black rectangles would stay in mask if we make less blur
https://1.bp.blogspot.com/-_RFXLBfMf_I/ ... blur_2.png

Here is example of such surface:
https://4.bp.blogspot.com/-zXbhxlS2fHw/ ... /map_2.png

So when I did the search of some surface, I have gaps there.

Making standard compare search is slow because it will search all pixels black and white. But if we would search only for white area and then check the areas within the outer borders of the white, then hopefully we could get better performance using shape search.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Locate subimage

Post by VanGog »

Looks like tomorrow I was too tired so I did not read snibgo's post completly or I don't remember what I read. So now again reading... Snibgo you said, you measured 0.12s with

Code: Select all

convert sub_image_water.png -scale 1x1! -scale 1024x632! zurich.png -compose Difference -composite -negate -grayscale Average zurich_water_2.png
But I have no tool to measure time exactly. It could be faster (?) just to search color using -fuzz and -fill to create mask. What is advantage of using difference? hypothetical thought - I see that one that If I would publish my script to be used by some-one else, then he maybe not aware what he is doing when editing batches. So it would be more simple to him to edit the sub image and then everything is done automatically. He just needs to copy a part of image and save it. And run script. But if I know how to edit scripts, than it could be faster to use -fuzz -fill. Or second possibility just to instruct the user who will use my script that he must resize the portion of image to size 1x1 in graphical editor and then just to run the batch (I suppose that such user will not know how to use convert - end of hypothetical thought).

Also to make the things clear. I updated the post from page 1. The yesterday's one. I added the code to make it clear what generated the water surface.

Code: Select all

for /f "usebackq tokens=1" %%o in (`convert map.png -format "%%wx%%h" info:`) do (
  set original_size=%%o )

convert map.png -format "%%wx%%h" info:
convert map.png -scale 25%% map_scaled.png
compare -metric RMSE -subimage-search map_scaled.png sub_image_water.png mask4.png
convert mask4-1.png -level 75%%,50%% mask_map_level_75_50_scaled_water.png
@REM Restore mask to original size
convert mask_map_level_75_50_scaled_water.png -resize "%original_size%!" mask_map_locations_water.png
I think snibgo talked about his script from his page, but I did not used it at all. I really don't understand how my script could generate so weird results (see page 1).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Locate subimage

Post by snibgo »

V7 has a "-read-mask" option. I don't know whether that can limit the search of either "compare" or "convert ... -format %[distortion] -compare".
VanGog wrote:I really don't understand how my script could generate so weird results (see page 1).
Look at your mask4.png. Where there is water, mask4.png is light, not dark. But your script then finds what is dark.
snibgo's IM pages: im.snibgo.com
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Locate subimage

Post by VanGog »

These images:
mask4-0
Image

mask-1 is dark

=>

mask_map_level_75_50_scaled_water.png:
Image

It is not clear to me why it works on different image an not now

mask4-1 is lighter, mask3-1 is darker. Is it because I searched for light color?

In that case the script could not be automated and would be unreliable. I should use -fuzz -fill which is more reliable.
Last edited by VanGog on 2016-06-02T03:57:36-07:00, edited 2 times in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Locate subimage

Post by snibgo »

Sorry, I meant mask4-1.png. This is the second output from "compare". It is light where the inputs are similar. It is dark where they are different.
snibgo's IM pages: im.snibgo.com
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Locate subimage

Post by VanGog »

mask4-1.png is this
Image
It is more light than dark. If I applied levels, the result is what you see. The yellow routes were selected not rivers

Maybe I should just try to create my own program using OpenCV as suggested here:
http://stackoverflow.com/questions/1817 ... this-image but it's time consuming choice
http://opencv.org/
Post Reply