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

查看:578
本文介绍了使用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 / Untitled%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.

谢谢!
Patrick。

Thanks! Patrick.

推荐答案

您是否看过 HoughLinesP 功能文档?

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

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天全站免登陆