如何通过tesseract OCR读取黑色背景图像上的黑色文本?

时间:2019-05-31 02:08:36

标签: python python-3.x ocr tesseract

我在黑色背景图像上有黑色文字,我想通过OCR进行阅读。不幸的是,OCR无法完美阅读。图像看起来像这样。 enter image description here 我想将小于(90,90,90,255)的RGBA值转换为(255,255,255,255),以便它变成黑白。转换它的代码是什么?

2 个答案:

答案 0 :(得分:3)

您需要做的是在让tesseract完成其工作之前,将整个图像变黑和变白。

阅读图像

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

将其设为灰度

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

”,它会使用Otsu的方法从图像中自动确定阈值,或者,如果您已经知道阈值,则可以使用:“

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

写入磁盘

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

Taken from here

答案 1 :(得分:0)

您可以通过简单的转换将灰色像素转换为白色像素。 如果您不想使用开放式简历,并且您的图像是一个通道(灰度级)的numpy数组:

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)
相关问题