使用 Opencv python 进行霍夫变换
Posted
技术标签:
【中文标题】使用 Opencv python 进行霍夫变换【英文标题】:Hough transformation with Open CV python 【发布时间】:2019-04-16 15:52:28 【问题描述】:我正在尝试在试管中应用霍夫概率变换,并且我已经有了经过良好过滤的图像(边缘)。
我需要识别管中间的任何这些直线(附图),以便我可以检测液位,但我不能这样做。有谁知道我该如何解决这个问题?
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)
我的实际结果在“houghlines”附图中。出现的是一条绿色的垂直线,但我需要一条水平线以便检测液位。
提前致谢。
tube
edges
houghlines
【问题讨论】:
【参考方案1】:我正在查看您的代码并修改了一些内容,并且看到了一些 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
祝你好运。
【讨论】:
谢谢萨尔瓦多。我需要的是明确定义的液位(没有垂直线)。你知道我们如何解决这个问题吗? 嗨,Jordan,我可以估计你的图像的液位,我附上结果的代码。【参考方案2】:看看是不是你需要的,问候。
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
祝你好运。
【讨论】:
感谢萨尔瓦多!这就是我需要的!以上是关于使用 Opencv python 进行霍夫变换的主要内容,如果未能解决你的问题,请参考以下文章
Python Opencv使用霍夫圆变换从二进制图像中检测圆
Python,OpenCV中的霍夫圆变换——cv2.HoughCircles()
学习 opencv---(13)opencv霍夫变换:霍夫线变换,霍夫圆变换