即使图像在Python的OpenCV中包含多行,Hough Line Transform也只能识别一行 [英] Hough Line Transform identifies only one line even though image contains many lines in OpenCV in Python

查看:139
本文介绍了即使图像在Python的OpenCV中包含多行,Hough Line Transform也只能识别一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used the Laplacian transform in OpenCV for edge detection and then used Hough Line Transform for detecting lines in it. These identified lines need to eventually removed from the image.

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)

我希望识别法案中的所有行:

I expect to identify all the lines in the Bill:

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

The results of the laplacian edge detection is as follows:

而Hough Line Transform返回的结果只能识别下图中绿线标记的一行:

whereas the results returned by Hough Line Transform identifies only one line as marked by the green line in the below image:

有人可以帮我弄清楚需要对代码进行哪些修改,以便所有粗体的水平/垂直线可以识别互联网帐单吗?

Could anyone help me figure out what modifications in code would be required so that all the bold horizontal/vertical lines of the Internet Bill could be identified?

推荐答案

在我看来,您仅在阅读行的第一个元素: / p>

It seems to me that you are only reading the first element of "lines" in:

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

因此,您仅绘制已找到的第一(最重要)线(最长,最粗)。我建议您尝试:

Thus, you are only drawing the first (most significant) line, that has been found ( the longest, most thick). I suggest you to try:

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

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

Further, I recommend to set minLineLength lower, if you still do not get the correct results.

这篇关于即使图像在Python的OpenCV中包含多行,Hough Line Transform也只能识别一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆