由于使用 PyTesseract 的背景颜色无法从屏幕截图中读取文本

Cannot read text from screenshot due to background color using PyTesseract

我正在尝试使用 pytesseract 从计算机/移动屏幕截图中提取和检测文本。 它工作正常,但在某些情况下,未检测到按钮文本可能是由于绿色背景。

原图

文本检测后的图像

这是我使用的代码:

d = pytesseract.image_to_data(img, output_type=Output.DICT)# img is an numpy nd array, i.e image read using OpenCV
n_boxes = len(d['level'])
for i in range(n_boxes):
    # eliminating blank characters
    if d['text'][i].strip() == '': continue
    else: print(d['text'][i])
    (x,y,w,h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
    cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)

plot_image(img)

尝试将图像二值化,使其变为黑白。 Binarization/Image 阈值处理是此类用例的常用图像处理方法。

这些链接可能会有帮助。

1.ImageThresholding-Opencv

2.Adaptive Thresholding

3.Text Binarization