改进纹理检测

2024-09-26 04:57:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用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)

但我有错误。 知道如何正确地将其转换为字符串吗


Tags: 代码图像image文本convertimgnpopen
1条回答
网友
1楼 · 发布于 2024-09-26 04:57:24

为了获得最佳效果,您应该传递黑白文本:

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

相关问题 更多 >