【问题标题】:How to extract text from an image with a slight background present?如何从存在轻微背景的图像中提取文本?
【发布时间】:2019-06-29 14:32:55
【问题描述】:

我正在寻找从图像中提取文本,我收到的输出不是很准确。我想知道是否可以采取任何其他步骤来更多地处理图像以提高此 OCR 的准确性。

我研究了一些处理图像和改进 OCR 结果的不同方法。图像很小,我可以将其稍微放大,但无济于事。

图像将始终是水平的,除了数字之外不会出现其他文本。最大数量将达到 55000。

有问题的图片示例:

图像处理后,我的图像在 X 和 Y 轴上放大了 4 倍。并且去除了一些饱和度,尽管这根本不会提高准确性。

image = self._process(scale=6, iterations=2)
text = pytesseract.image_to_string(image, config="--psm 7")

我的处理方法是这样做的:

# Resize and desaturate.
image = cv2.resize(image, None, fx=scale, fy=scale, 
interpolation=cv2.INTER_CUBIC)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply dilation and erosion.
kernel = np.ones((1, 1), np.uint8)
image = cv2.dilate(image, kernel, iterations=iterations)
image = cv2.erode(image, kernel, iterations=iterations)

return image

预期:“10411”

实际值多变,通常是无法识别的字符串,或者解析了一些数字但准确率太低无法使用。

【问题讨论】:

    标签: python opencv processing ocr


    【解决方案1】:

    我没有使用 OCR 的经验,但我认为您走在正确的轨道上:增加图像大小以使算法有更多像素可以使用,并增加数字和背景之间的区别。

    我添加的技巧:thresholding 图像,它创建了一个仅保留白色像素的蒙版。有一些不是数字的白色斑点,所以我使用findContours 将那些不需要的斑点涂成黑色。

    结果:

    代码:

    import numpy as np 
    import cv2
    # load image
    image = cv2.imread('number.png')
    # resize image
    image = cv2.resize(image,None,fx=5, fy=5, interpolation = cv2.INTER_CUBIC)
    # create grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # perform threshold
    retr, mask = cv2.threshold(gray_image, 230, 255, cv2.THRESH_BINARY)
    # find contours
    ret, contours, hier = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    # draw black over the contours smaller than 200 - remove unwanted blobs
    for cnt in contours:
        # print contoursize to detemine threshold
        print(cv2.contourArea(cnt))
        if cv2.contourArea(cnt) < 200:
             cv2.drawContours(mask, [cnt], 0, (0), -1)
    
    #show image
    cv2.imshow("Result", mask)
    cv2.imshow("Image", image)
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    【讨论】:

    • 这太棒了,我还想补充一点,Tesseract 带有一些预配置的选项,可能有助于从图像中提取数字。对于与我相似的图像,使用额外的命令行参数:--psm 7 nobatch digits 大大提高了我的准确性!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 2015-07-31
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多