如何使用linux控制台搜索图像的子图像? [英] How to search an image for subimages using linux console?

查看:170
本文介绍了如何使用linux控制台搜索图像的子图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 位置-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

这是第二个更有用的输出文件位置-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 +%的位置确定匹配并将所有像素转换为文本,以便我们搜索单词白色

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控制台搜索图像的子图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆