How to detect water level in a transparent container?

社会主义新天地 提交于 2021-02-19 08:59:20

问题


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?


回答1:


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.

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

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

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:



来源:https://stackoverflow.com/questions/33238267/how-to-detect-water-level-in-a-transparent-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!