如何使用 linux 控制台在图像中搜索子图像?
How to search an image for subimages using linux console?
我必须使用控制台在较大图像中搜索较小图像的出现。结果我想收到它的图像坐标。有哪些可能的解决方案?
我听说过 ImageMagick,但不是很了解它是如何工作的。如果足够了,那么我将不胜感激示例命令。
谢谢。
这是一个小例子,您可以了解它是如何工作的...
首先是我们的针形图
现在做一个干草堆,绿色和蓝色 - 非常时尚:-)
convert -size 256x256 gradient:lime-blue haystack.png
现在大海捞针两根,一次一根,没什么特别的:
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
compare -metric RMSE -subimage-search haystack.png needle.png locations.png > /dev/null 2>&1
这是第二个更有用的输出文件locations-1.png
。它是黑色的,IM 确定没有匹配项,并且逐渐接近白色,ImageMagick 越确定存在匹配项。
现在寻找 IM 95% 以上确定匹配的位置,并将所有像素转换为文本,以便我们可以搜索单词 white
。
convert locations-1.png -threshold 95% txt: | grep white
输出是这样的,这意味着 ImageMagick 已经在 30,5 和 100,150 处找到了针 - 正是我们隐藏它们的地方!告诉过你这是 魔法!
30,5: (255,255,255) #FFFFFF white
100,150: (255,255,255) #FFFFFF white
这是完整的脚本,因此您可以 运行 并使用它:
#!/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
我必须使用控制台在较大图像中搜索较小图像的出现。结果我想收到它的图像坐标。有哪些可能的解决方案?
我听说过 ImageMagick,但不是很了解它是如何工作的。如果足够了,那么我将不胜感激示例命令。
谢谢。
这是一个小例子,您可以了解它是如何工作的...
首先是我们的针形图
现在做一个干草堆,绿色和蓝色 - 非常时尚:-)
convert -size 256x256 gradient:lime-blue haystack.png
现在大海捞针两根,一次一根,没什么特别的:
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
compare -metric RMSE -subimage-search haystack.png needle.png locations.png > /dev/null 2>&1
这是第二个更有用的输出文件locations-1.png
。它是黑色的,IM 确定没有匹配项,并且逐渐接近白色,ImageMagick 越确定存在匹配项。
现在寻找 IM 95% 以上确定匹配的位置,并将所有像素转换为文本,以便我们可以搜索单词 white
。
convert locations-1.png -threshold 95% txt: | grep white
输出是这样的,这意味着 ImageMagick 已经在 30,5 和 100,150 处找到了针 - 正是我们隐藏它们的地方!告诉过你这是 魔法!
30,5: (255,255,255) #FFFFFF white
100,150: (255,255,255) #FFFFFF white
这是完整的脚本,因此您可以 运行 并使用它:
#!/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