Remove noisy lines from an image

℡╲_俬逩灬. 提交于 2019-11-30 14:59:04

问题


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.

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


回答1:


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'})

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




回答2:


You can do that using createLineSegmentDetector(), a function from opencv

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:

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.



来源:https://stackoverflow.com/questions/54028493/remove-noisy-lines-from-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!