即使图像在Python中的OpenCV中包含许多行,Hough Line Transform也只识别一行

时间:2016-07-12 09:24:46

标签: python opencv image-processing computer-vision hough-transform

我在OpenCV中使用拉普拉斯变换进行边缘检测,然后使用霍夫线变换检测其中的线。这些已识别的线条最终需要从图像中删除。

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('Feb_16-0.jpg',0)
kernel = np.ones((1,1),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
blur = cv2.GaussianBlur(opening,(1,1),0)
ret3,th4 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 
laplacian = cv2.Laplacian(th4,cv2.CV_8UC1)
cst = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(laplacian,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
    cv2.line(cst,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imwrite('houghlines5.jpg',cst)

我希望找出条例草案中的所有内容: Internet Bill

拉普拉斯边缘检测的结果如下:

The edge detection results

而Hough Line Transform返回的结果只标识了下图中绿线标记的一条线: Transformed Internet Bill

任何人都可以帮我弄清楚代码中需要进行哪些修改,以便识别互联网法案的所有粗体水平/垂直线?

1 个答案:

答案 0 :(得分:8)

在我看来,你只是阅读" line"的第一个元素。在:

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

因此,您只绘制了已发现的第一条(最重要的)线(最长,最厚)。我建议你试试:

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

此外,如果您仍未获得正确的结果,我建议将minLineLength设置为较低。

相关问题