5

I have black text on black background image and I want to read it through OCR. Unfortunately, OCR can not read it perfectly. The image look like this. enter image description here I want to convert RGBA value that less than (90, 90, 90, 255) to (255, 255, 255, 255) so it turn B & W. What's the code to convert it?

2 Answers 2

6

What you need to do is make the whole image black and white before letting tesseract do its job.

Read image

import cv2
im_gray = cv2.imread('your_image_here', cv2.IMREAD_GRAYSCALE)

Make it grayscale

(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

"which determines the threshold automatically from the image using Otsu's method, or if you already know the threshold you can use:"

thresh = 127
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]

Write to disk

cv2.imwrite('bw_image.png', im_bw)

Taken from here

1
  • When you use an THRESH_OTSU, automatically THRESH is automatically considered zero. cv2.threshold(im_gray, THRESH=0, 255, cv2.THRESH_OTSU) Dec 19, 2021 at 19:09
0

You can transform your gray pixels in white pixels with a simple transformation. If you don't want to use open cv and your image is one channel (gray scale) numpy array:

threshold = 60 # try something between 30 and 150
vect_func = np.vectorize(lambda x: 0 if x == threshold else 255)
black_white_img = vect_func(gray_scale_image)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.