问题描述
我必须使用控制台在较大的图像中搜索较小图像的出现.结果我想接收它的图像坐标.有哪些可能的解决方案?
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?
我听说过 ImageMagick,但并不真正了解它是如何工作的.如果它足够了,那么我会很感激一个示例命令.
I heard about ImageMagick, but not really understand how it works. If it`s enough, then I would appreciate an example command.
谢谢.
推荐答案
这里有一个小例子,你可以看看它是如何工作的......
Here's a little example so you can see how it works...
首先,我们的针头图像
现在做一个干草堆,绿色和蓝色 - 非常时尚:-)
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
现在在大海捞针中搜索,会产生两个输出文件,locations-0.png
和locations-1.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
这是第二个更有用的输出文件locations-1.png
.在 IM 确定没有匹配项的情况下,它是黑色的,并且随着 ImageMagick 越确定存在匹配项,它会逐渐接近白色.
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.
现在查找 IM 95+% 确定匹配的位置,并将所有像素转换为文本,以便我们可以搜索单词 white
.
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
输出是这样的,这意味着 ImageMagick 在 30,5 和 100,150 处找到了针——正是我们将它们藏起来的地方!告诉过你这是魔法!
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
这篇关于如何使用 linux 控制台在图像中搜索子图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!