gpt4 book ai didi

python - 如何使用 Tesseract 提高图像质量以从图像中提取文本

转载 作者:行者123 更新时间:2023-12-01 08:23:02 24 4
gpt4 key购买 nike

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

有人可以帮助我吗?

enter image description here

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

最佳答案

鉴于@barny 的评论,我不知道这是否有效,但您可以尝试下面的代码。我创建了一个脚本,用于选择显示区域并将其变形为直接图像。接下来是字符的黑白蒙版的阈值,结果会稍微清理一下。

尝试一下是否可以提高识别率。如果确实如此,还要查看中间阶段,以便您了解发生的所有情况。

更新:看来 Tesseract 更喜欢白底黑字,反转并扩大了结果。

结果:

enter image description here

更新结果:

enter image description here

代码:

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()

关于python - 如何使用 Tesseract 提高图像质量以从图像中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54497882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com