使用Open CV python进行霍夫变换

时间:2019-04-16 15:52:28

标签: python numpy opencv

我正在尝试在管中应用霍夫概率变换,并且我已经有一个过滤良好的图像(边缘)。

我需要识别出管中部的任何这些直线(附图),以便我可以检测液位,但我不能这样做。有谁知道我该怎么解决?

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)

我的实际结果在附图的“强度线”中。出现的是绿色和垂直线,但我需要水平线,以便可以检测液位。

提前谢谢。

tube

edges

houghlines

2 个答案:

答案 0 :(得分:0)

我正在查看您的代码并进行了一些修改,并且看到了一些OpenCV enter link description here的文档。

我得到了这个结果,我不知道这是否是您所需要的。

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

在这里enter link description here

寻找类似的问题

祝你好运。

答案 1 :(得分:0)

请问您是否需要它。

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

祝你好运。