使用 OpenCV 进行水平线检测 [英] Horizontal Line detection with OpenCV

查看:97
本文介绍了使用 OpenCV 进行水平线检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从来自文档"的图像中找到水平线和垂直线.文档是从合同中扫描出来的页面,因此线条看起来就像您在表格或合同块中看到的一样.

I am trying to find horizontal and vertical lines from an image which came from a "document". The documents are scanned pages from contracts and so the lines look like what you would see in a table or in a contract block.

我一直在尝试使用 OpenCV 来完成这项工作.OpenCV 中的 Hough 变换实现似乎对这项工作很有用,但我找不到任何参数组合可以让它干净地找到垂直线和水平线.我试过有和没有边缘检测.没运气.如果有人做过类似的事情,我很想知道如何做.

I have been trying OpenCV for the job. The Hough transform implementation in OpenCV seemed useful for the job, but I could not find any combination of parameters that would allow it to cleanly find the vertical and horizontal lines. I tried with and without edge detection. No luck. If anyone has done anything similar I'm interested in knowing how.

在此处查看我在 OpenCV 中使用 HoughP 进行实验之前和之后的图像.这是我能做的最好的,http://dl.dropbox.com/u/3787481/无标题%201.png

See here an image of my before and after experimentation with HoughP in OpenCV. It's the best I could do, http://dl.dropbox.com/u/3787481/Untitled%201.png

所以现在我想知道是否可以使用另一种变换来可靠地找到水平线和垂直线(最好也是虚线).

So now I'm wondering whether there is another kind of transform I could use which would allow me to reliably find horizontal and vertical lines (and preferably dashed lines too).

我知道这个问题是可以解决的,因为我有 Nuance 和 ABBYY OCR 工具,它们可以可靠地提取水平线和垂直线并返回线的边界框.

I know this problem is solvable because I have Nuance and ABBYY OCR tools which can both reliably extract horizontal and vertical lines and return me the bounding box of the lines.

谢谢!帕特里克.

推荐答案

您是否看过来自 HoughLinesP 函数文档?

Have you seen a code sample from HoughLinesP function documentation?

我认为您可以将其用作算法的起点.要选择水平线和垂直线,您只需按线角过滤掉其他线.

I think you can use it as starting point for your algorithm. To pick horizontal an vertical lines you just need to filter out other lines by line angle.

更新:

正如我所见,您需要在页面上找到的不是线条而是水平和垂直边缘.对于此任务,您需要结合多个处理步骤才能获得良好的结果.

As I see you need to find not the lines but horizontal an vertical edges on the page. For this task you need to combine several processing steps to get good results.

对于您的图像,我可以通过将 Canny 边缘检测与 HoughLinesP 相结合来获得良好的结果.这是我的代码(我使用过 python,但我认为你明白这个想法):

For your image I'm able to get good results by combining Canny edge detection with HoughLinesP. Here is my code (I've used python, but I think you see the idea):

img = cv2.imread("C:/temp/1.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 80, 120)
lines = cv2.HoughLinesP(edges, 1, math.pi/2, 2, None, 30, 1);
for line in lines[0]:
    pt1 = (line[0],line[1])
    pt2 = (line[2],line[3])
    cv2.line(img, pt1, pt2, (0,0,255), 3)
cv2.imwrite("C:/temp/2.png", img)

结果如下:

这篇关于使用 OpenCV 进行水平线检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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