如何让HoughLines识别出这张照片中的其余部分?

时间:2015-06-13 00:38:59

标签: python opencv hough-transform canny-operator

在下面的图片中,您可以看到我能够很好地识别水平线,但是垂直线并没有如此出色。特别是,没有看到网格的中间线,并且侧线被透支(即彼此连接)。

以下是创建它的代码:

img = cv2.imread('./p6.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
threshhold, threshhold_img = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
edges = cv2.Canny(threshhold_img, 150, 200, 3, 5)
lines = cv2.HoughLinesP(edges,1,np.pi/180,500, minLineLength = 600, maxLineGap = 75)[0].tolist()

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

当我调整了不同的参数时,我最终遇到的情况就是中间垂直线(在表格中JEXXY之前结束的那条)从底部延伸到顶部的情况。第三个网格。除非我放松了这些参数,以至于几乎每一行都被绘制出来,包括代表前三个网格顶部的yiersan的那些,我无法获得代码看到定义网格内部的中间垂直线。

我该如何解决这个问题?

Image with hough lines drawn in

*已更新为使用THRESH_BINARY_INV而不使用canny *

img = cv2.imread('./p6.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
threshhold, threshhold_img = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV)
lines = cv2.HoughLinesP(edges,1,np.pi/180,500, minLineLength = 600, maxLineGap = 75)[0].tolist()

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

Intermediate picture for threshold_img using THRESH_BINARY_INV

After doing THRESH_BINARY_INV, this is the lines output

*更新:使用BINARY和OTSU *

添加了阈值img

threshold_img with THRESH_BINARY + THRESH_OTSU

1 个答案:

答案 0 :(得分:2)

我认为在你的情况下发生的事情是很难找到一组参数,这些参数可以同时创建水平线和垂直线而没有一些线条和#34;偷窃"来自其他一些方面的投票。你可以在这里做一个快速的技巧,只强制检测垂直线,同时专注于你的主导水平线:

# Vertical lines
lines = cv2.HoughLinesP(
    threshhold_img, 1, np.pi, threshold=100, minLineLength=100, maxLineGap=1)

# Horizontal lines
lines2 = cv2.HoughLinesP(
    threshhold_img, 1, np.pi / 2, threshold=500, minLineLength=500, maxLineGap=1)

给了我:

enter image description here