Piotr Sobolewski
Piotr Sobolewski

Reputation: 2134

Create readable words after binarization

I am using opencv with Python to cleanup images to be readable for tesseract. I have a black and white image, and after adaptive thresholding, it doesn't look good enough. There is a lot of paper noise and letters are not so clean. How can I fix it?

adaptiveThreshold method:

cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

source:

source

after adaptiveThreshold:

after adaptiveThreshold

also I tried erosion and dilation:

kernel = np.ones((2,2), np.uint8)
e = cv2.erode(roi_sharpen, kernel, iterations=1))
d = cv2.dilate(roi_sharpen, kernel, iterations=1))

results:

enter image description here

enter image description here

Upvotes: 0

Views: 183

Answers (1)

cxyzs7
cxyzs7

Reputation: 1227

Since you noticed that there's lots of noise, it's always a good idea to try some smoothing to the image.

For example, you can apply a gaussian filter to the original image

smooth_img = cv.GaussianBlur(img, (5, 5), 0, 0)
bin_img = cv.adaptiveThreshold(smooth_img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)

I tried a few kernel sizes, seems 5 by 5 kernel gave the best result on this example

Binary image with Gaussian blur first

If you don't like the small dots in the image, you can further apply a median filter to remove them

clean_img = cv.medianBlur(bin_img, 3)

You will get

After median filter

There are also lots of parameter tuning for Tesseract too, if you don't get satisfying result, you might want to try a few different Tesseract settings.

Upvotes: 2

Related Questions