繁体   English   中英

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

[英]How to read black text on black background image through tesseract OCR?

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

您需要做的是在让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)

从这里取

您可以通过简单的转换将灰色像素转换为白色像素。 如果您不想使用开放式简历,并且图像是一个通道(灰度级)的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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM