简体   繁体   English

从图像中删除嘈杂的线条

[英]Remove noisy lines from an image

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).我想对它们进行一些预处理,以去除不需要的噪音(扭曲书写的线条),以便我可以将它们与 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.将具有两个或更少相邻黑色像素的所有黑色像素变为白色。

That seems a bit complicated for me since I am beginner to computer vision and opencv.这对我来说似乎有点复杂,因为我是计算机视觉和 opencv 的初学者。
Any help would be appreciated, thank you.任何帮助将不胜感激,谢谢。

Detecting lines like these is what the path opening was invented for.像这样的检测线就是路径开口的发明目的。 DIPlib has an implementation (disclosure: I implemented it there). DIPlib有一个实现(披露:我在那里实现了它)。 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 diplib as dip
import matplotlib.pyplot as pp

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

Here we first inverted the image because that makes other things later easier.在这里,我们首先反转图像,因为这使以后的其他事情更容易。 If not inverting, use a path closing instead.如果不反转,请改用路径闭合。 The lines image: lines图:

线

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

最后结果

You can do that using createLineSegmentDetector() , a function from opencv您可以使用来自 opencv 的函数createLineSegmentDetector()来做到这一点

import cv2

#Read gray image
img = cv2.imread("lines.png",0)

#Create default parametrization LSD
lsd = cv2.createLineSegmentDetector(0)

#Detect lines in the image
lines = lsd.detect(img)[0] #Position 0 of the returned tuple are the detected lines

#Draw the detected lines
drawn_img = lsd.drawSegments(img,lines)

#Save the image with the detected lines
cv2.imwrite('lsdsaved.png', drawn_img)

在此处输入图片说明
The next part of the code will delete only the lines which their length is more than 50 pixels:代码的下一部分将仅删除长度超过 50 像素的行:

for element in lines:

  #If the length of the line is more than 50, then draw a white line on it
  if (abs(int(element[0][0]) - int(element[0][2])) > 50 or abs(int(element[0][1]) - int(element[0][3])) > 50): 

    #Draw the white line
    cv2.line(img, (int(element[0][0]), int(element[0][1])), (int(element[0][2]), int(element[0][3])), (255, 255, 255), 12)

#Save the final image
cv2.imwrite('removedzz.png', img)

在此处输入图片说明

Well, it didn't work perfectly with the current image, but it may give better results with different images.好吧,它不能与当前图像完美配合,但它可能会为不同的图像提供更好的结果。 You can adjust the length of the lines to remove and the thickness of the white lines to draw insteaad of the removed lines.您可以调整要删除的线的长度和要绘制的白线的粗细,以代替已删除的线。
I hope it helps.我希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM