【问题标题】:How to improve text extraction from an image?如何改进从图像中提取文本?
【发布时间】:2018-08-03 12:35:14
【问题描述】:

我正在使用 pytesseract 从图像中提取文本。在用 pytesseract 提取文本之前,我使用 Pillow 和 cv2 来降噪和增强图像:

import numpy as np
import pytesseract
from PIL import Image, ImageFilter, ImageEnhance
import cv2

img = cv2.imread('ss.png')

img = cv2.resize(img, (0,0), fx=3, fy=3)
cv2.imwrite("new.png", img)

img1 = cv2.imread("new.png", 0)

#Apply dilation and erosion
kernel = np.ones((2, 2), np.uint8)
img1 = cv2.dilate(img1, kernel, iterations=1)
img1 = cv2.erode(img1, kernel, iterations=1)

img1 = cv2.adaptiveThreshold(img1,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,2)

cv2.imwrite("new1.png", img1)
img2 = Image.open("new1.png")

#Enhance the image
img2 = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
img2 = enhancer.enhance(2)
img2.save('new2.png')

result = pytesseract.image_to_string(Image.open("new2.png"))
print(result)

我大多都能得到很好的结果,但是当我使用一些低质量/分辨率的图像时,我没有得到预期的输出。我可以在我的代码中改进它吗?

例子:

输入:

new1.png:

new2.png:

我从控制台得到的字符串是 play。我可以在算法中进行哪些更改,以便提取整个字符串?

任何帮助将不胜感激。

【问题讨论】:

    标签: python opencv image-processing ocr pillow


    【解决方案1】:

    这是一个迟到的答案,但我刚刚遇到这个。在使用 pytesseract 从图像中提取文本之前,我们可以使用 Pillowcv2 来降低噪声并增强图像。我希望它会在将来对某人有所帮助。

    #import required library
    
    src_path = "C:/Users/chethan/Desktop/"
    
    def get_string(img_path):
        # Read image with opencv
        img = cv2.imread(img_path)
    
        # Convert to gray
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
        # Apply dilation and erosion to remove some noise
        kernel = np.ones((1, 1), np.uint8)
        img = cv2.dilate(img, kernel, iterations=1)
        img = cv2.erode(img, kernel, iterations=1)
    
        # Write image after removed noise
        cv2.imwrite(src_path + "removed_noise.png", img)
    
        #  Apply threshold to get image with only black and white
        #img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
    
        # Write the image after apply opencv to do some ...
        cv2.imwrite(src_path + "thres.png", img)
    
        # Recognize text with tesseract for python
        result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))
    
     # Recognize text with tesseract for python
        result = pytesseract.image_to_string(Image.open(img_path))
    
    #     Remove template file
    #     os.remove(temp)
    
        return result
    
    print(get_string(src_path + "dummy.png"))
    

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      相关资源
      最近更新 更多