【问题标题】:How to search an image for subimages using linux console?如何使用 linux 控制台在图像中搜索子图像?
【发布时间】:2015-08-06 22:54:36
【问题描述】:

我必须使用控制台在较大的图像中搜索较小图像的出现。结果我想接收它的图像坐标。有哪些可能的解决方案?

我听说过 ImageMagick,但并不真正了解它是如何工作的。如果它足够了,那么我会很感激一个示例命令。

谢谢。

【问题讨论】:

标签: linux image search console imagemagick


【解决方案1】:

这里有一个小例子,你可以看看它是如何工作的......

首先,我们的针头图像

现在做一个干草堆,绿色和蓝色 - 非常时尚:-)

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

【讨论】:

  • “干草堆”和“针”...大声笑 :-)
猜你喜欢
  • 2022-01-19
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-14
  • 2014-04-01
  • 1970-01-01
  • 2021-11-08
相关资源
最近更新 更多