如何通过 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)
"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]
写入磁盘
cv2.imwrite('bw_image.png', im_bw)
Taken from here
您可以通过简单的转换将灰色像素转换为白色像素。
如果你不想使用 open cv 并且你的图像是一个通道(灰度)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)
我在黑色背景图片上有黑色文字,我想通过 OCR 读取它。不幸的是,OCR 无法完美读取它。图像看起来像这样。
您需要做的是在让 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)
"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]
写入磁盘
cv2.imwrite('bw_image.png', im_bw)
Taken from here
您可以通过简单的转换将灰色像素转换为白色像素。 如果你不想使用 open cv 并且你的图像是一个通道(灰度)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)