以编程方式删除图像中的所有线条和边框(保留文本)的方法是什么? [英] What's the way to remove all lines and borders in image(keep texts) programmatically?

查看:375
本文介绍了以编程方式删除图像中的所有线条和边框(保留文本)的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Tesseract OCR从图像中提取文本。
目前,使用原始输入图像(如下所示),输出质量非常差(约50%)。但是当我尝试删除输入图像中的所有线条和边框(使用photoshop)时,输出提高了很多(~90) %)。
那么有没有办法以编程方式删除图像中的所有行和边框(保留文本)(使用OpenCV,Image magick,..)?

I 'm trying to extract text from an image using Tesseract OCR. Currently, with original input image(as below), output's very poor quality(about 50%).But when I try to remove all lines and borders in input image(using photoshop), output improve a lot(~90%). So is there any way to remove all lines and borders in image(keep texts) programmatically(using OpenCV, Image magick,..) ?

原始图像:

Original Image:

期待图片:

Expect Image:

推荐答案

不使用OpenCV,只是终端中的一行ImageMagick,但它可以让你知道如何在OpenCV中做到这一点。 ImageMagick安装在大多数Linux发行版上,适用于OSX和Windows。

Not using OpenCV, but just a one-liner of ImageMagick in the Terminal, but it may give you an idea how to do it in OpenCV. ImageMagick is installed on most Linux distros and is available for OSX and Windows.

概念的关键是创建一个新图像,其中每个像素设置为中位数左边的100个相邻像素和右边的100个相邻像素。这样,具有许多黑色水平邻居(即水平黑线)的像素在输出图像中将是白色的。然后在垂直方向上应用相同的处理以删除垂直线。

The crux of the concept is to create a new image where each pixel is set to the median of the 100 neighbouring pixels to its left and the 100 neighbouring pixels to its right. That way, pixels that have lots of horizontal neighbours that are black (i.e. horizontal black lines) will be white in the output image. Then the same processing is applied in the vertical direction to remove vertical lines.

您在终端中输入的命令将是:

The command that you type into the Terminal will be:

convert input.png                                                 \
   \( -clone 0 -threshold 50% -negate -statistic median 200x1 \)  \
   -compose lighten -composite                                    \
   \( -clone 0 -threshold 50% -negate -statistic median 1x200 \)  \
   -composite result.png

第一行表示加载原始图像。

The first line says to load your original image.

第二行开始一些一边处理复制原始图像,对其进行阈值并将其反转,然后计算任一边的所有相邻像素100的中值。

The second line starts some "aside-processing" that copies the original image, thresholds it and inverts it, then the median of all neighbouring pixels 100 either side is calculated.

第三行然后获取第二行的结果并将其合成在原始图像上,选择每个位置的较亮像素 - 即我的水平线遮罩已变白的那些。

The third line then takes the result of the second line and composites it over the original image, choosing the lighter of the pixels at each location - i.e. the ones that my horizontal line mask has whitened out.

接下来的两行再次做同样的事情,但垂直方向为垂直线。

The next two lines do the same thing again but oriented vertically for vertical lines.

结果是这样的:

如果我与原始图片不同,就像这样,我可以看到它做了什么:

If I difference that with your original image, like this, I can see what it did:

convert input.png result.png -compose difference -composite diff.png

我想,如果你想删除更多的线条,你可以实际模糊差异图像并将其应用到原始图像。当然,您也可以使用过滤器长度和阈值等。

I guess, if you wanted to remove a bit more of the lines, you could actually blur the difference image a little and apply that to the original. Of course, you can play with the filter lengths and the thresholds and stuff too.

这篇关于以编程方式删除图像中的所有线条和边框(保留文本)的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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