【问题标题】:How improve image quality to extract text from image using Tesseract如何使用 Tesseract 提高图像质量以从图像中提取文本
【发布时间】:2019-02-02 21:51:17
【问题描述】:

我正在尝试在下面的代码中使用 Tessract 来提取图像的两行。我尝试提高图像质量,但没有效果。

谁能帮帮我?

from PIL import Image, ImageEnhance, ImageFilter
import pytesseract

img = Image.open(r'C:\ocr\test00.jpg')
new_size = tuple(4*x for x in img.size)
img = img.resize(new_size, Image.ANTIALIAS)
img.save(r'C:\\test02.jpg', 'JPEG')


print( pytesseract.image_to_string( img ) )

【问题讨论】:

  • 您是否尝试旋转图像以使文本水平?
  • 是的。我也尝试将其设置为具有高对比度的黑白。
  • 我似乎记得在某处看到 Tesserarct 不喜欢点阵文本 - 你能在例如opencv 所以字符更像是连续笔画?

标签: python opencv text tesseract python-tesseract


【解决方案1】:

鉴于@barny 的评论,我不知道这是否可行,但您可以尝试下面的代码。我创建了一个脚本来选择显示区域并将其扭曲成一个直的图像。接下来是字符的黑白蒙版的阈值,结果被清理了一点。

试试它是否能提高识别度。如果是这样,还请查看中间阶段,以便您了解所发生的一切。

更新:似乎 Tesseract 更喜欢白色背景上的黑色文本,反转和放大结果。

结果:

更新结果:

代码:

import numpy as np 
import cv2
# load image
image = cv2.imread('disp.jpg')

# create grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# perform threshold
retr, mask = cv2.threshold(gray_image, 190, 255, cv2.THRESH_BINARY)

# findcontours
ret, contours, hier = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# select the largest contour
largest_area = 0
for cnt in contours:
    if cv2.contourArea(cnt) > largest_area:
        cont = cnt
        largest_area = cv2.contourArea(cnt)

# find the rectangle (and the cornerpoints of that rectangle) that surrounds the contours / photo
rect = cv2.minAreaRect(cont)
box = cv2.boxPoints(rect)
box = np.int0(box)

#### Warp image to square
# assign cornerpoints of the region of interest
pts1 = np.float32([box[2],box[3],box[1],box[0]])
# provide new coordinates of cornerpoints
pts2 = np.float32([[0,0],[500,0],[0,110],[500,110]])

# determine and apply transformationmatrix
M = cv2.getPerspectiveTransform(pts1,pts2)
tmp = cv2.warpPerspective(image,M,(500,110))

 # create grayscale
gray_image2 = cv2.cvtColor(tmp, cv2.COLOR_BGR2GRAY)
# perform threshold
retr, mask2 = cv2.threshold(gray_image2, 160, 255, cv2.THRESH_BINARY_INV)

# remove noise / close gaps
kernel =  np.ones((5,5),np.uint8)
result = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kernel)

#draw rectangle on original image
cv2.drawContours(image, [box], 0, (255,0,0), 2)

# dilate result to make characters more solid
kernel2 =  np.ones((3,3),np.uint8)
result = cv2.dilate(result,kernel2,iterations = 1)

#invert to get black text on white background
result = cv2.bitwise_not(result)

#show image
cv2.imshow("Result", result)
cv2.imshow("Image", image)

cv2.waitKey(0)
cv2.destroyAllWindows()

【讨论】:

  • 哇!感谢您的关注。即使它不起作用,我也会从你的解决方案中学到很多东西。我会在接下来的几天内尝试并返回这里通知
  • JD,我迫不及待地想要测试它。一开始它没有用,但我相信我可以改进以从你停止的地方开始获得解决方案。真的很有趣你是如何处理图像的,我从中学到了很多东西。非常感谢您的代码。
  • 我刚刚在几个地方读到 example 说 Tesseract 在黑色背景上的白色文本确实做得很好。所以我添加了一条反转结果的行。你能再试一次吗?
  • 刚刚有了另一个想法。我还添加了一个dilation 步骤,使字符更加立体。我认为这会有很大帮助。我很好奇它是否有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-30
  • 2020-04-21
相关资源
最近更新 更多