改善tesseract检测

问题描述 投票:1回答:1

我尝试使用Tesseract从该图像中提取文本。enter image description here我尝试过的代码:

img = Image.open('downloadedpng.jpeg').convert('L')
ret,img = cv2.threshold(np.array(img), 125, 255, cv2.THRESH_BINARY)
img = Image.fromarray(img.astype(np.uint8))
print(pytesseract.image_to_string(img))

我得到的输出:re vie我已经尝试使用以下代码进行侵蚀和扩张:

img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)

但是我遇到了错误。知道如何将其正确转换为字符串吗?

python python-3.x opencv python-tesseract
1个回答
2
投票

为了获得最佳效果,您应该在白底白字之间传递:

import cv2
from PIL import Image

img = cv2.imread('1.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.bitwise_not(img) # <- invert
ret, thresh = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)
im = Image.fromarray(thresh.astype("uint8"))
print(pytesseract.image_to_string(im))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.