How to remove noise from an image in opencv (see the input picture)? I want to achieve that in the output I will get white background and black text only.
CodePudding user response:
Assuming grayscale image, you can partially eliminate the noise like this:
# thresholding
thresh, thresh_img = cv.threshold(img, 128, 255, 0, cv.THRESH_BINARY)
# erode the image to *enlarge* black blobs
erode = cv.erode(thresh_img, cv.getStructuringElement(cv.MORPH_ELLIPSE, (3,3)))
# fill in the black blobs that are not surrounded by white:
_, filled, _, _ = cv.floodFill(erode, None, (0,0), 255)
# binary and with the threshold image to get rid of the thickness from erode
out = (filled==0) & (thresh_img==0)
# also
# out = cv.bitwise_and(filled, thresh_img)
The output is not clean (some dark blobs between the text lines, which can be further removed by thresholding the sizes of connected components), but this should be a good start: