首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何提高此图像中的OCR精度?

如何提高此图像中的OCR精度?
EN

Stack Overflow用户
提问于 2021-05-03 04:45:13
回答 1查看 391关注 0票数 1

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

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

这是我的代码:

代码语言:javascript
复制
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那样工作。

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

代码语言:javascript
复制
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

你有什么方法可以提高准确度吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-03 17:00:58

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

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

代码语言:javascript
复制
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)

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

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

代码语言:javascript
复制
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
代码语言:javascript
复制
----------------------------------------
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
----------------------------------------
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67360958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档