如何改善图像的文本提取?

时间:2017-08-07 14:57:24

标签: python opencv image-processing ocr pillow

我正在使用 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)

我大多取得了不错的成绩,但是当我使用一些低质量/分辨率的图像时,我没有得到预期的输出。我可以在代码中改进吗?

示例:

输入:ss

new1.png:new1

new2.png:enter image description here

我从控制台获得的字符串是播放。我可以在算法中更改什么,以便提取整个字符串?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

这是一个很晚的答案,但我刚遇到这个问题。在使用 pytesseract 从图像中提取文本之前,我们可以使用 Pillow cv2 来减少噪声并增强图像。我希望它将来对某人有帮助。

#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"))