如何检测透明容器中的水位? [英] How to detect water level in a transparent container?

查看:68
本文介绍了如何检测透明容器中的水位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用opencv-python库进行液位检测.到目前为止,我已经能够将图像转换为灰度并应用精巧边缘检测已识别出容器.

I am using opencv-python library to do the liquid level detection. So far I was able to convert the image to gray scale and applying canny edge detection the container has been identified.

import numpy as np
import cv2
import math
from matplotlib import pyplot as plt
from cv2 import threshold, drawContours


img1 = cv2.imread('botone.jpg')
kernel = np.ones((5,5),np.uint8)
#convert the image to grayscale
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(imgray,120,230)

我需要知道如何从这一阶段开始寻找水位.我应该尝试机器学习,还是有其他可用的选项或算法?

I need to know how to find water level from this stage. Should I try machine learning, or is there any other option or algorithm available?

我采取了一种在边缘检测到的图像中找出水平线的方法.如果水平线超过某个阈值,我可以将其视为水平.但是结果不一致.

I took an approach of finding out the horizontal line in the edge detected image. If the horizontal line crosses certain threshold I can consider it as level.But the result is not consistent.

我想知道我是否可以使用其他方法或白皮书作为参考?

I want to know if there are any other approaches i can go with or white papers for reference?

推荐答案

我不知道如何使用 numpy opencv 做到这一点,因为我使用ImageMagick(已安装在大多数Linux发行版中,并且适用于OSX和Windows),但是该概念应该适用.

I don't know how you would do that with numpy and opencv, because I use ImageMagick (which is installed on most Linux distros and is avilable for OSX and Windows), but the concept should be applicable.

首先,我可能会选择一个可以旋转以找到水平边缘的Sobel滤镜-即定向滤镜.

First, I would probably go for a Sobel filter that is rotated to find horizontal edges - i.e. a directional filter.

convert chemistry.jpg -morphology Convolve Sobel:90 sobel.jpg

然后,我可能会考虑添加一个Hough变换以在水平边缘检测到的图像中找到线条.所以,我的一线客机在Terminal/shell中看起来像这样:

Then I would probably look at adding in a Hough Transform to find the lines within the horizontal edge-detected image. So, my one-liner looks like this in the Terminal/shell:

convert chemistry.jpg -morphology Convolve Sobel:90 -hough-lines 5x5+30 level.jpg

如果我添加一些调试功能,则可以看到Sobel滤波器的系数:

If I add in some debug, you can see the coefficients of the Sobel filter:

convert chemistry.jpg -define showkernel=1 -morphology Convolve Sobel:90 -hough-lines 5x5+30 sobel.jpg
Kernel "Sobel@90" of size 3x3+1+1 with values from -2 to 2
Forming a output range from -4 to 4 (Zero-Summing)
 0:         1         2         1
 1:         0         0         0
 2:        -1        -2        -1

如果我再添加一些调试,则可以看到检测到的行的坐标:

If I add in some more debug, you can see the coordinates of the lines detected:

convert chemistry.jpg -morphology Convolve Sobel:90 -hough-lines 5x5+30 -write lines.mvg level.jpg

lines.mvg

# Hough line transform: 5x5+30
viewbox 0 0 86 196
line 0,1.52265 86,18.2394  # 30      <-- this is the topmost, somewhat diagonal line
line 0,84.2484 86,82.7472  # 40      <-- this is your actual level
line 0,84.5 86,84.5  # 40            <-- this is also your actual level
line 0,94.5 86,94.5  # 30            <-- this is the line just below the surface
line 0,93.7489 86,95.25  # 30        <-- so is this
line 0,132.379 86,124.854  # 32      <-- this is the red&white valve(?)
line 0,131.021 86,128.018  # 34
line 0,130.255 86,128.754  # 34
line 0,130.5 86,130.5  # 34
line 0,129.754 86,131.256  # 34
line 0,192.265 86,190.764  # 86
line 0,191.5 86,191.5  # 86
line 0,190.764 86,192.265  # 86
line 0,192.5 86,192.5  # 86

正如我在评论中所说,请考虑考虑为您的实验提供更好的照明-使用不同的彩色灯,更多的散射灯,不同的方向灯.另外,如果您的实验随着时间的流逝而发生,则可以考虑查看图像之间的差异,以查看哪条线在移动...

As I said in my comments, please also think about maybe lighting your experiment better - either with different coloured lights, more diffuse lights, different direction lights. Also, if your experiment happens over time, you could consider looking at differences between images to see which line is moving...

以下是原始图片上方的线条:

Here are the lines on top of your original image:

这篇关于如何检测透明容器中的水位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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