I have to search for the occurrence of a smaller image in a larger one using the console. As result I want to receive it`s image coordinates. What solutions are possible?
I heard about ImageMagick, but not really understand how it works. If it`s enough, then I would appreciate an example command.
Thank you.
Here's a little example so you can see how it works...
First, our needle image
Now make a haystack, green and blue - very stylish :-)
convert -size 256x256 gradient:lime-blue haystack.png
Now hide two needles in the haystack, one at a time, nothing fancy:
convert haystack.png needle.png -geometry +30+5 -composite haystack.png
convert haystack.png needle.png -geometry +100+150 -composite haystack.png
Now search for the needles in the haystack, two output files will be produced, locations-0.png
and locations-1.png
compare -metric RMSE -subimage-search haystack.png needle.png locations.png > /dev/null 2>&1
This is the second, more useful output file locations-1.png
. It is black where IM is sure there is no match and progressively nearer to white the more certain ImageMagick is that there is a match.
Now look for locations where IM is 95+% certain there is a match and convert all pixels to text so we can search for the word white
.
convert locations-1.png -threshold 95% txt: | grep white
The output is this, meaning ImageMagick has found the needles at 30,5 and 100,150 - exactly where we hid them! Told you it was Magic!
30,5: (255,255,255) #FFFFFF white
100,150: (255,255,255) #FFFFFF white
Here is the entire script so you can run it and play with it:
#!/bin/bash
convert -size 256x256 gradient:lime-blue haystack.png # make our haystack
convert haystack.png needle.png -geometry +30+5 -composite haystack.png # hide our needle near top-left
convert haystack.png needle.png -geometry +100+150 -composite haystack.png # hide a second needle lower down
# Now search for the needles in the haystack...
# ... two output files will be produced, "locations-0.png" and "locations-1.png"
compare -metric RMSE -subimage-search haystack.png needle.png locations.png > /dev/null 2>&1
# Now look for locations where IM is 95% certain there is a match
convert locations-1.png -threshold 95% txt: | grep white
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With