协助字符识别问题

时间:2019-02-10 09:11:02

标签: python ocr tesseract python-tesseract

在字符分割之后,我使用了tesseract从每个轮廓提取文本,但是tesseract给我错误的结果。有没有办法提高tesseract输出的质量?

enter image description here

1 个答案:

答案 0 :(得分:-1)

在简单的预处理可以为您提供所需结果的情况下,您无需培训tesseract。

def remove_small_dots_from_image(im):
    _, black_and_white = cv2.threshold(im, 127, 255, cv2.THRESH_BINARY_INV)

    n_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(black_and_white, None, None, None, 8, cv2.CV_32S)
    sizes = stats[1:, -1]  # get CC_STAT_AREA component
    img2 = np.zeros(labels.shape, np.uint8)

    for i in range(0, n_labels - 1):
        if sizes[i] >= 50:  # filter small dotted regions
            img2[labels == i + 1] = 255

    return cv2.bitwise_not(img2)

image = cv2.imread('image.png', cv2.IMREAD_GRAYSCALE)
image = remove_small_dots_from_image(image)
#add white border around the image because tesseract would fail to guess font size otherwise
image = cv2.copyMakeBorder(image, 30, 30, 30, 30, cv2.BORDER_CONSTANT, None, 255)
print(pytesseract.image_to_string(image, config='--psm 7'))

该代码为您提供所需的输出

  

2

enter image description here