删除图像中的嘈杂线条 [英] Remove noisy lines from an image

查看:167
本文介绍了删除图像中的嘈杂线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有随机线条的噪点图像,如下所示:

我想对它们进行一些预处理,以消除不想要的噪声(使书写变形的线条),以便可以将它们与OCR(Tesseract)一起使用.
我想到的想法是,使用扩散法消除噪声,然后使用侵蚀法修复第二部分中的文字缺失部分.
为此,我使用了以下代码:

I have images that are noised with some random lines like the following one:

I want to apply on them some preprocessing in order to remove the unwanted noise ( the lines that distort the writing) so that I can use them with OCR (Tesseract).
The idea that came to my mind is to use dilation to remove the noise then use erosion to fix the missing parts of the writing in a second step.
For that, I used this code:

import cv2
import numpy as np

img = cv2.imread('linee.png', cv2.IMREAD_GRAYSCALE)
kernel = np.ones((5, 5), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
cv2.imwrite('delatedtest.png', img)

不幸的是,扩张效果不佳,噪声线仍然存在.

Unfortunately, the dilation didn't work well, The noise lines are still existing.


我尝试更改内核的形状,但情况变得更糟:部分或全部删除了文字.
我还发现了答案,说可以通过


I tried changing the kernel shape, but it got worse: the writing were partially or completely deleted.
I also found an answer saying that it is possible to remove the lines by

将具有两个或更少相邻黑色像素的所有黑色像素变为白色.

turning all black pixels with two or less adjacent black pixels to white.

由于我是计算机视觉和opencv的初学者,所以这对我来说有点复杂.
任何帮助将不胜感激,谢谢.

That seems a bit complicated for me since I am beginner to computer vision and opencv.
Any help would be appreciated, thank you.

推荐答案

开路. PyDIP 有一个实现(公开:我在此处实现;还请注意,您必须从以下位置安装PyDIP源,因为我们尚未创建二进制发行版).或者,您可以尝试使用我上面链接的论文的实现.该实现没有我在下面使用的受限"模式.

Detecting lines like these is what the path opening was invented for. PyDIP has an implementation (disclosure: I implemented it there; also note that you'll have to install PyDIP from sources as we haven't yet created a binary distribution). As an alternative, you can try using the implementation by the authors of the paper that I linked above. That implementation does not have the "constrained" mode that I use below.

以下是如何使用它的快速演示:

Here is a quick demo for how you can use it:

import PyDIP as dip
import matplotlib.pyplot as pp

img = 1 - pp.imread('/home/cris/tmp/DWRTF.png')
lines = dip.PathOpening(img, length=300, mode={'constrained'})

在这里,我们首先反转图像,因为这样会使以后的事情变得更容易.如果不求反,则改用闭合路径. lines图片:

Here we first inverted the image because that makes other things later easier. If not inverting, use a path closing instead. The lines image:

接下来,我们减去行.一个小区域的开口将删除路径开口所滤除的行中的几个孤立像素:

Next we subtract the lines. A small area opening removes the few isolated pixels of the line that were filtered out by the path opening:

text = img - lines
text = dip.AreaOpening(text, filterSize=5)

但是,我们现在在文本中有空白.填补这些并非微不足道.这是一个快速而肮脏的尝试,您可以将其用作起点:

However, we've now made gaps in the text. Filling these up is not trivial. Here is a quick-and-dirty attempt, which you can use as a starting point:

lines = lines > 0.5
text = text > 0.5
lines -= dip.BinaryPropagation(text, lines, connectivity=-1, iterations=3)
img[lines] = 0

这篇关于删除图像中的嘈杂线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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