如何处理和提取图像中的文本

时间:2019-08-28 15:33:12

标签: python image opencv image-processing python-tesseract

我正在尝试使用python cv2从图像中提取文本。结果很可悲,我想不出一种方法来改进我的代码。 我认为在提取文本之前需要对图像进行处理,但不确定如何处理。

Sample image

我试图将其转换为黑白,但没有运气。

import cv2
import os
import pytesseract
from PIL import Image
import time

pytesseract.pytesseract.tesseract_cmd='C:\\Program Files\\Tesseract-OCR\\tesseract.exe'

cam = cv2.VideoCapture(1,cv2.CAP_DSHOW)

cam.set(cv2.CAP_PROP_FRAME_WIDTH, 8000)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 6000)

while True:
    return_value,image = cam.read()
    image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    image = image[127:219, 508:722]
    #(thresh, image) = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    cv2.imwrite('test.jpg',image)
    print('Text detected: {}'.format(pytesseract.image_to_string(Image.open('test.jpg'))))
    time.sleep(2)

cam.release()
#os.system('del test.jpg')

2 个答案:

答案 0 :(得分:3)

在执行文本提取之前进行预处理以清洁图像会有所帮助。这是一种简单的方法

  • 将图像转换为灰度并锐化图像
  • 自适应阈值
  • 执行形态学操作以清洁图像
  • 反转图像

首先我们将其转换为灰度,然后使用sharpening kernel

来锐化图像

enter image description here

接下来,我们自适应阈值以获取二进制图像

enter image description here

现在我们执行morphological transformations来平滑图像

enter image description here

最后我们将图像反转

enter image description here

import cv2
import numpy as np

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
sharpen_kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpen = cv2.filter2D(gray, -1, sharpen_kernel)
thresh = cv2.threshold(sharpen, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)
result = 255 - close

cv2.imshow('sharpen', sharpen)
cv2.imshow('thresh', thresh)
cv2.imshow('close', close)
cv2.imshow('result', result)
cv2.waitKey()

答案 1 :(得分:0)

尝试一下

    from PIL import image
    import PIL.image
    from pytesseract import image_to_string
     import pytesseract

     pytesseract.pytesseract.tesseract_cmd = '_________' 
          #tesseract executable path -- if not added to 
            environment path, if added ignore

       Output = 
         pytesseract.image_to_string(PIL.image.open('image 
            wants to read'). convert ("RBG"), Lang='eng')
       Print (output)