python - 如何让 HoughLines 识别这张图片中的其余线条?

标签 python opencv hough-transform canny-operator

在下图中,您可以看到我能够很好地识别水平线,但垂直线就不太好。特别是,没有看到网格的中间线,并且边线被 overdraw (即相互连接)。

这是创建它的代码:

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

最佳答案

我认为你的情况是很难找到一组参数来同时创建水平线和垂直线,而不会有一些线从其他一些线“窃取”选票。你可以在这里做一个快速的技巧,只强制检测垂直线,而稍后关注你的主要水平线:

# 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

关于python - 如何让 HoughLines 识别这张图片中的其余线条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30814018/

相关文章:

python - 操作错误 : unable to open database file: sqlalchemy

python - 自适应网格细化 - Python

python - OpenCV Python 中的变形操作

c++ - 在 cv::Mat 中捕获部分屏幕

java - 使用 hough 进行瞳孔检测的最佳参数? java opencv

opencv - HoughLinesP 与 IplImage

python - cv2.VideoWriter 只生成空文件

python - Keras:显示多标签回归中每个标签的损失

python - 如何使用 Python 将 OpenCV 图像编码为字节

c++ - 我们如何修改 OpenCV 并生成新的 DLL