在双色调背景下使用 pytesseract 从图像中提取文本

发布于 2025-01-16 10:48:05 字数 1033 浏览 3 评论 0原文

我正在尝试使用 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:

Original Image

This is the image after applying threshold:

Image with threshold

Console Output:

20 hours

20 hours

Bhours

Console Output Image

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼亦尘 2025-01-23 10:48:05

由于您正在处理在深色背景上包含白色字符的图像,因此建议在使用 pytesseract 之前将其反转。

这是使用 inverted_grey_image = cv2.bitwise_not(grey_image) 完成的。

然后,您可以在 threshold 中调整阈值:_,bt = cv2.threshold(inverted_grey_image, 140 ,255,cv2.THRESH_BINARY)

下面是完整代码:

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

inverted_grey_image = cv2.bitwise_not(grey_image)

_,bt = cv2.threshold(inverted_grey_image, 140 ,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)  

它返回:

20小时

20小时

3小时

编辑:我目前正在处理类似的 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:

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

inverted_grey_image = cv2.bitwise_not(grey_image)

_,bt = cv2.threshold(inverted_grey_image, 140 ,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)  

It returns:

20 hours

20 hours

3 hours

EDIT: I am currently dealing with a similar OCR problem: you may want to check this recent post for inspiration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文