gpt4 book ai didi

python - 如何提高此图像中的 OCR 准确度?

转载 作者:行者123 更新时间:2023-12-05 01:30:06 24 4
gpt4 key购买 nike

我将使用 Python 中的 OpenCV 和 pytesseract 的 OCR 从图片中提取文本。我有这样一张图片:

Input

然后我编写了一些代码来从该图片中提取文本,但它没有足够的准确性来正确提取文本。

这是我的代码:

import cv2
import pytesseract

img = cv2.imread('photo.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_,img = cv2.threshold(img,110,255,cv2.THRESH_BINARY)

custom_config = r'--oem 3 --psm 6'
text = pytesseract.image_to_string(img, config=custom_config)
print(text)

cv2.imshow('pic', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

我已经测试了 cv2.adaptiveThreshold,但它不像 cv2.threshold 那样工作。

最后,这是我的结果,它与图片中的结果不一样:

Color Yellow RBC/hpf 4-6
Appereance Semi Turbid WBC/hpf 2-3
Specific Gravity 1014 Epithelial cells/Lpf 1-2
PH 7 Bacteria (Few)
Protein Pos(+) Casts Negative
Glucose Negative Mucous (Few)
Keton Negative
Blood Pos(+)
Bilirubin Negative
Urobilinogen Negative
Nigitesse 5 ed eg ative

有什么办法可以提高准确率吗?

最佳答案

我真的很惊讶,看到这个明显的偏差,结果已经很好了。但是,这不是最后一行的实际问题,而是影子!这是您的阈值图像:

Thresholded

因此,pytesseract 没有机会从最后一行中正确检测到任何有意义的内容。让我们尝试去除阴影,跟随 Dan Mašek's answer here ,然后让 Otsu 进行阈值处理:

import cv2
import numpy as np
import pytesseract

# Read input image, convert to grayscale
img = cv2.imread('NiVUK.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Remove shadows, cf. https://stackoverflow.com/a/44752405/11089932
dilated_img = cv2.dilate(gray, np.ones((7, 7), np.uint8))
bg_img = cv2.medianBlur(dilated_img, 21)
diff_img = 255 - cv2.absdiff(gray, bg_img)
norm_img = cv2.normalize(diff_img, None, alpha=0, beta=255,
norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)

# Threshold using Otsu's
work_img = cv2.threshold(norm_img, 0, 255, cv2.THRESH_OTSU)[1]

# Tesseract
custom_config = r'--oem 3 --psm 6'
text = pytesseract.image_to_string(work_img, config=custom_config)
print(text)

去除阴影的阈值图像如下所示:

Shadow-removed, thresholded

而且,最终输出对我来说似乎是正确的:

Color Yellow RBC/hpf 4-6
Appereance Semi Turbid WBC/hpf 2-3
Specific Gravity 1014 Epithelial cells/Lpf 1-2
PH 7 Bacteria (Few)
Protein Pos(+) Casts Negative
Glucose Negative Mucous (Few)
Keton Negative
Blood Pos(+)
Bilirubin Negative
Urobilinogen Negative
Nitrite Negative
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
PyCharm: 2021.1.1
NumPy: 1.20.2
OpenCV: 4.5.1
pytesseract: 4.00.00alpha
----------------------------------------

关于python - 如何提高此图像中的 OCR 准确度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67360958/

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