加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

如何使用linux控制台搜索图像的子图像?

发布时间:2020-12-13 23:57:39 所属栏目:Linux 来源:网络整理
导读:我必须使用控制台搜索较大图像中较小图像的出现.结果我想收到它的图像坐标.有什么解决方案? 我听说过ImageMagick,但不太了解它是如何工作的.如果它足够了,那么我会很感激一个示例命令. 谢谢. 解决方法 这是一个小例子,你可以看到它是如何工作的…… 首先,我
我必须使用控制台搜索较大图像中较小图像的出现.结果我想收到它的图像坐标.有什么解决方案?

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

谢谢.

解决方法

这是一个小例子,你可以看到它是如何工作的……

首先,我们的针图像

enter image description here

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

convert -size 256x256 gradient:lime-blue haystack.png

enter image description here

现在在大海捞针中隐藏两根针,一次一只,没什么特别的:

convert haystack.png needle.png -geometry +30+5 -composite haystack.png 
convert haystack.png needle.png -geometry +100+150 -composite haystack.png

enter image description here

现在搜索大海捞针,将生成两个输出文件,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是匹配.

enter image description here

现在查找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)  #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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读