【问题标题】:How to read black text on black background image through tesseract OCR?如何通过 tesseract OCR 读取黑色背景图像上的黑色文本?
【发布时间】:2019-10-16 16:12:16
【问题描述】:

我在黑色背景图像上有黑色文本,我想通过 OCR 读取它。不幸的是,OCR 不能完美地读取它。图像看起来像这样。 我想将小于 (90, 90, 90, 255) 的 RGBA 值转换为 (255, 255, 255, 255) 使其变为黑白。转换它的代码是什么?

【问题讨论】:

    标签: python python-3.x ocr tesseract


    【解决方案1】:

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

    【讨论】:

    • 当您使用 THRESH_OTSU 时,自动将 THRESH 自动视为零。 cv2.threshold(im_gray, THRESH=0, 255, cv2.THRESH_OTSU)
    【解决方案2】:

    您可以通过简单的转换将灰色像素转换为白色像素。 如果您不想使用 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)
    

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2017-04-23
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2019-12-13
      • 1970-01-01
      相关资源
      最近更新 更多