如何提高该图像的 OCR 准确性?

FAT*_*EGH 2 python ocr opencv image-processing python-tesseract

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

输入

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

这是我的代码:

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()
Run Code Online (Sandbox Code Playgroud)

我已经测试过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
Run Code Online (Sandbox Code Playgroud)

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

Han*_*rse 9

事实上,看到这种明显的偏差,我感到非常惊讶,结果已经有多好了。但是,这不是最后一行的实际问题,而是阴影!这是你的阈值图像:

\n

阈值

\n

因此,pytesseract没有机会正确检测最后一行中任何有意义的内容。让我们尝试去除阴影,按照Dan Ma\xc5\xa1ek\'s 在这里的回答,并让 Otsu 进行阈值处理:

\n
import cv2\nimport numpy as np\nimport pytesseract\n\n# Read input image, convert to grayscale\nimg = cv2.imread(\'NiVUK.jpg\')\ngray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n\n# Remove shadows, cf. https://stackoverflow.com/a/44752405/11089932\ndilated_img = cv2.dilate(gray, np.ones((7, 7), np.uint8))\nbg_img = cv2.medianBlur(dilated_img, 21)\ndiff_img = 255 - cv2.absdiff(gray, bg_img)\nnorm_img = cv2.normalize(diff_img, None, alpha=0, beta=255,\n                         norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)\n\n# Threshold using Otsu\'s\nwork_img = cv2.threshold(norm_img, 0, 255, cv2.THRESH_OTSU)[1]\n\n# Tesseract\ncustom_config = r\'--oem 3 --psm 6\'\ntext = pytesseract.image_to_string(work_img, config=custom_config)\nprint(text)\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

阴影去除、阈值化

\n

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

\n
Color Yellow RBC/hpf 4-6\nAppereance Semi Turbid WBC/hpf 2-3\nSpecific Gravity 1014 Epithelial cells/Lpf 1-2\nPH 7 Bacteria (Few)\nProtein Pos(+) Casts Negative\nGlucose Negative Mucous (Few)\nKeton Negative\nBlood Pos(+)\nBilirubin Negative\nUrobilinogen Negative\nNitrite Negative\n
Run Code Online (Sandbox Code Playgroud)\n
----------------------------------------\nSystem information\n----------------------------------------\nPlatform:      Windows-10-10.0.16299-SP0\nPython:        3.9.1\nPyCharm:       2021.1.1\nNumPy:         1.20.2\nOpenCV:        4.5.1\npytesseract:   4.00.00alpha\n----------------------------------------\n
Run Code Online (Sandbox Code Playgroud)\n