【问题标题】:Why will tesseract not detect this letter?为什么 tesseract 检测不到这个字母?
【发布时间】:2020-12-04 21:34:28
【问题描述】:

我正在尝试检测这封信,但它似乎无法识别它。

import cv2
import pytesseract as tess
img = cv2.imread("letter.jpg")
imggray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(tess.image_to_string(imggray))

这是有问题的图片:

【问题讨论】:

  • 能否请您添加错误信息?
  • 它识别它是什么?

标签: python opencv3.0


【解决方案1】:

图像的预处理(例如反转它)应该会有所帮助,您也可以利用 pytesseract image_to_string 配置选项。

例如,类似这样的事情:

import pytesseract 
import cv2 as cv
import requests
import numpy as np
import io

# I read this directly from imgur
response = requests.get('https://i.stack.imgur.com/LGFAu.jpg') 
nparr = np.frombuffer(response.content, np.uint8)
img = cv.imdecode(nparr, cv.IMREAD_GRAYSCALE)
# simple inversion as preprocessing
neg_img = cv.bitwise_not(img)
# invoke tesseract with options
text = pytesseract.image_to_string(neg_img, config='--psm 7')

print(text)

应该正确解析字母。

查看相关问题以获取有关预处理和 tesseract 选项的更多信息:
Why does pytesseract fail to recognise digits from image with darker background?
Why does pytesseract fail to recognize digits in this simple image?
Why does tesseract fail to read text off this simple image?

【讨论】:

    【解决方案2】:

    @Davide Fiocco 的回答绝对正确。

    我只是想用adaptive-thresholding展示另一种方法

    当您申请adaptive-thesholding 时,结果将是:

    现在当你阅读它时:

    txt = pytesseract.image_to_string(thr, config="--psm 7")
    print(txt)
    

    结果:

    B
    

    代码:


    import cv2
    import pytesseract
    
    img = cv2.imread("LGFAu.jpg")
    gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    thr = cv2.adaptiveThreshold(gry, 252, cv2.ADAPTIVE_THRESH_MEAN_C,
                                cv2.THRESH_BINARY_INV, 11, 2)
    txt = pytesseract.image_to_string(thr, config="--psm 7")
    print(txt)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 2020-12-12
      相关资源
      最近更新 更多