如何在仪表的位图图像中找到仪表值

2024-04-18 22:48:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚学习了Python,并成功地创建了一个运行在Raspberry Pi上的脚本,该脚本使用连接的USB相机拍照。你知道吗

以下是示例图像:

enter image description here

现在我需要检测仪表的值吗?你知道我该怎么做吗。我所能想到的只是一些愚蠢的算法。你知道吗

一个多步骤的ImageMagick程序?B/W,去除噪音,设法把仪表和背景隔离开来???你知道吗

感谢您的帮助,但如果您有Python代码,我将倍感感激!你知道吗

我有个想法:

  • 使用Photoshop移除仪表,只留下带有值的圆,从而创建一个模板图像
  • 使用ImageMagic,从模板中减去“新”图像
  • 以某种方式解释产生的差异图像?你知道吗

Tags: 图像程序脚本算法模板示例pi仪表
1条回答
网友
1楼 · 发布于 2024-04-18 22:48:41

下面是一种使用Imagemagick的连接组件从图像中获取箭头的方法,这些组件需要Imagemagick 6.8.9.10或更高版本。我相信OpenCV也有相似之处。你知道吗

http://magick.imagemagick.org/script/connected-components.php

我们首先将图像转换为灰度,然后设置阈值,以获得尽可能少的黑色整个箭头。然后,我们使用面积阈值的连接组件,使得最小的黑色区域是箭头,并使用其ID(=42)来提取该图像。你知道吗

以下Unix语法在我的Mac OSX Sierra上使用Imagemagick 6.9.10.8 Q16运行。你知道吗

这里有一种方法,假设您运行了一次以获取id,并确保箭头项是最后一个黑色项(在本例中是第二大的黑色项)。你知道吗

convert image.png -colorspace gray -threshold 45% -type bilevel +write tmp.gif \
-define connected-components:verbose=true \
-define connected-components:area-threshold=100 \
-define connected-components:mean-color=true \
-define connected-components:keep=42 \
-connected-components 4 \
arrow.png

我加了+写tmp.gif文件要显示灰度阈值图像:

enter image description here

Objects (id: bounding-box centroid area mean-color):
  5: 94x94+5+7 51.7,53.5 6711 gray(255)
  1: 104x104+0+2 41.6,66.0 2157 gray(0)
  0: 104x54+0+0 50.1,11.3 1412 gray(255)
  175: 37x42+67+64 92.7,94.1 536 gray(255)
  42: 16x49+45+23 51.2,53.6 208 gray(0)


以下是最终结果:

enter image description here

或者,我们可以使区域阈值相同,并找到列表中最后一项的ID,然后在图像中提取该区域。 如上所述,我们正在提取面积较小的区域,即ID=42,而不知道它是42。你知道吗

OLD_IFS=$IFS
IFS=$'\n'
arr=(`convert image.png -colorspace gray -threshold 45% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:area-threshold=100 \
-define connected-components:mean-color=true \
-connected-components 4 null: | tail -n +2 | sed 's/^[ ]*//'`)
IFS=$OLD_IFS
num=${#arr[*]}
last_id=`echo "${arr[$num-1]}" | cut -d: -f1`
convert image.png -colorspace gray -threshold 45% -type bilevel \
-define connected-components:mean-color=true \
-define connected-components:keep=$last_id \
-connected-components 4 \
arrow2.png


enter image description here

我不知道有什么好方法可以自动获得所需的区域阈值,它应该是箭头中黑色像素的数量。但在这种情况下,你可以假设它是第二大区域。这样我们就可以去掉区域阈值线,只需通过它是第二个颜色为灰色(0)的对象来找到它:

OLD_IFS=$IFS
IFS=$'\n'
arr=(`convert image.png -colorspace gray -threshold 45% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 null: | tail -n +2 | sed 's/^[ ]*//'`)
IFS=$OLD_IFS
num=${#arr[*]}
k=1
for ((i=0; i<num; i++)); do
color=`echo "${arr[$i]}" | cut -d\  -f5`
id=`echo "${arr[$i]}" | cut -d\  -f1 | sed 's/[:]*$//'`
echo "k=$k; color=$color; id=$id;"
if [ $k -eq 2 -a "$color" = "gray(0)" ]; then
break;
elif [ "$color" = "gray(0)" ]; then
k=$((k+1))
fi
done
convert image.png -colorspace gray -threshold 45% -type bilevel \
-define connected-components:mean-color=true \
-define connected-components:keep=$id \
-connected-components 4 \
arrow3.png


enter image description here

此时,您可以将该图像转换为极坐标并定位该点的角度。例如:

convert arrow.png -virtual-pixel white +distort depolar 0 depolar.png


enter image description here

https://www.imagemagick.org/Usage/distorts/#polar

查看棋盘格示例,输出从左侧的-180变为右侧的+180。输入图像的中心沿着输出图像的顶部缝合。我相信零角是从中心向下的。顺时针方向从0到顶部的-180。逆时针从下到顶部从0到+180。你知道吗

相关问题 更多 >