如何改善从图像中提取文本? [英] How to improve text extraction from an image?

查看:114
本文介绍了如何改善从图像中提取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pytesseract 从图像中提取文本.在使用pytesseract提取文本之前,我使用Pillow和cv2来减少噪点并增强图像:

I am using pytesseract to extract text from images. Before extracting text with pytesseract, I use Pillow and cv2 to reduce noise and enhance the image:

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)

大多数情况下,我会得到很好的结果,但是当我使用一些质量低下/分辨率较低的图像时,却没有得到预期的输出.我可以在代码中对此进行改进吗?

I mostly get good results, but when I use some low quality/resolution images, I do not get the expected output. Can I improve this in my code?

示例:

输入:

new1.png:

new1.png:

new2.png:

new2.png:

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

The string that I get from the console is play. What could I change in my algorithm, so that I get the whole string extracted?

任何帮助将不胜感激.

推荐答案

这是一个很晚的答案,但我只是碰到了这一点.在使用 pytesseract 从图像中提取文本之前,我们可以使用 Pillow cv2 来减少噪声并增强图像.我希望这会在将来对某人有所帮助.

This is a late answer, but I just came across this. we can use Pillow and cv2 to reduce noise and enhance the image before extracting text from images using pytesseract. I hope it would help someone in future.

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

这篇关于如何改善从图像中提取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆