在双色调背景下使用 pytesseract 从图像中提取文本
我正在尝试使用 Python 上的 pytesseract 从图像中提取文本。这是我要提取文本的图像:
这是应用后的图像阈值:
控制台输出:
20 hours
20 hours
Bhours
这是我正在使用的代码:
from pytesseract import *
import cv2
path = r"path where image is located" #path of image
folderPath = r"path for saving output image"
grey_image = cv2.imread(path,0) #import image
_,bt = cv2.threshold(grey_image, 150 ,255,cv2.THRESH_BINARY) #variable means binary threshold
cv2.imwrite(folderPath + "\\" + "test.png", bt) #Saving result
imageout = pytesseract.image_to_string(bt) #Convert image to text
print(imageout) #Print text in console
我一直在尝试不同的阈值范围,但是仍然无法得到精确的输出。
为了获得精确的结果,您有什么建议?
I'm trying to extract text from an image using pytesseract on Python. This is the image where I want to extract the text:
This is the image after applying threshold:
Console Output:
20 hours
20 hours
Bhours
This is the code I'm using:
from pytesseract import *
import cv2
path = r"path where image is located" #path of image
folderPath = r"path for saving output image"
grey_image = cv2.imread(path,0) #import image
_,bt = cv2.threshold(grey_image, 150 ,255,cv2.THRESH_BINARY) #variable means binary threshold
cv2.imwrite(folderPath + "\\" + "test.png", bt) #Saving result
imageout = pytesseract.image_to_string(bt) #Convert image to text
print(imageout) #Print text in console
I've been trying different ranges of thresholding but still can't get a precise output.
What do you suggest for getting a precise result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您正在处理在深色背景上包含白色字符的图像,因此建议在使用 pytesseract 之前将其反转。
这是使用
inverted_grey_image = cv2.bitwise_not(grey_image)
完成的。然后,您可以在
threshold
中调整阈值:_,bt = cv2.threshold(inverted_grey_image, 140 ,255,cv2.THRESH_BINARY)
下面是完整代码:
它返回:
编辑:我目前正在处理类似的 OCR 问题:您可能需要检查这篇最近的文章来获取灵感。
Because you are dealing with an image containing white characters over a dark background, it is recommended to invert it prior using pytesseract.
This is done using
inverted_grey_image = cv2.bitwise_not(grey_image)
.You may then adjust the threshold in
threshold
:_,bt = cv2.threshold(inverted_grey_image, 140 ,255,cv2.THRESH_BINARY)
Below is the full code:
It returns:
EDIT: I am currently dealing with a similar OCR problem: you may want to check this recent post for inspiration.