jod*_*yme 4 python ocr tesseract python-3.x
我在黑色背景图像上有黑色文本,我想通过 OCR 阅读它。不幸的是,OCR 无法完美读取。图像看起来像这样。
我想将小于 (90, 90, 90, 255) 的 RGBA 值转换为 (255, 255, 255, 255) 所以它变成黑白。转换它的代码是什么?
您需要做的是在让 tesseract 完成其工作之前将整个图像设为黑白。
读取图像
import cv2
im_gray = cv2.imread('your_image_here', cv2.IMREAD_GRAYSCALE)
Run Code Online (Sandbox Code Playgroud)
使其灰度化
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
Run Code Online (Sandbox Code Playgroud)
“它使用 Otsu 的方法从图像中自动确定阈值,或者如果您已经知道可以使用的阈值:”
thresh = 127
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
Run Code Online (Sandbox Code Playgroud)
写入磁盘
cv2.imwrite('bw_image.png', im_bw)
Run Code Online (Sandbox Code Playgroud)