0

I'm trying to apply the hough probabilistic transform in a tube, and I already have a well-filtered image (edges).

My need is to recognize any of these straight lines (attached figure) that are in the middle of the tube so i can detect the liquid level, but I can not do this. Do anyone know how i can solve this?

import cv2
import numpy as np

img = cv2.imread('tube.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray.png',gray)

edges = cv2.Canny(gray,350,720,apertureSize = 3)
cv2.imwrite('edges.png',edges)

minLineLength = 30
maxLineGap = 0

lines = cv2.HoughLinesP(edges,1,np.pi/180,10,minLineLength,maxLineGap)

for x1,y1,x2,y2 in lines[0]:
    cv2.line(img,(x1,y1),(x2,y2),(0,255,0),4)

cv2.imwrite('houghlines.png',img)

My actual results are in the 'houghlines' attached figure. What appears is a green and vertical line, but i need a horizontal one so i can detect the liquid level.

thanks in advance.

tube

edges

houghlines

glibdud
  • 7,550
  • 4
  • 27
  • 37
jordan
  • 1
  • 2

2 Answers2

0

I was view your code and modified some things and I saw a little of the documentation of OpenCV enter link description here.

I have this result, I don't know if it's what you need.

import cv2
import numpy as np

img = cv2.imread('tube.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray.png',gray)

edges = cv2.Canny(gray,350,720, apertureSize = 3)
cv2.imwrite('edges.png',edges)

rho = 1  # distance resolution in pixels of the Hough grid
theta = np.pi / 180  # angular resolution in radians of the Hough grid
threshold = 10  # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50  # minimum number of pixels making up a line
max_line_gap = 20  # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0  # creating a blank to draw lines on

# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                        min_line_length, max_line_gap)

for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)

lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
cv2.imwrite('houghlines.png',lines_edges)

houghlines.png

Look for a similar problem here enter link description here

Good luck.

0

Check if it's what you need, regards.

import cv2
import numpy as np
import math


img = cv2.imread('tube.png')
#img = cv2.resize(img,(360,480))

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,350,720, apertureSize = 3)
#cv2.imshow("edges", edges)

rho = 1
#theta = np.pi / 180 #CHANGE FOR MATH.pi/1
threshold = 10  # minimum number of votes (intersections in Hough grid cell)
min_line_length = 2  # minimum number of pixels making up a line
max_line_gap = 480  # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0  # creating a blank to draw lines on
lines = cv2.HoughLinesP(edges, rho, math.pi/1, threshold, np.array([]), 
min_line_length, max_line_gap);

#coordinates
dot1 = (lines[0][0][0],lines[0][0][1])
dot2 = (lines[0][0][2],lines[0][0][3])
dot3 = (lines[0][0][1],lines[0][0][1])


cv2.line(img, dot1, dot2, (255,0,0), 3)
cv2.line(img, dot1, dot3, (0,255,0), 3)
cv2.imshow("output", img)


length = lines[0][0][1] - lines[0][0][3]

print ('Pixels Level', length)

if cv2.waitKey(0) & 0xFF == 27:
  cv2.destroyAllWindows()

lines img

terminal output

Good luck.