Pytesseract无法识别图像中的数字

时间:2019-09-05 01:37:51

标签: python opencv image-processing ocr python-tesseract

Pytesseract无法识别数字6和8。

当前将6识别为5,将5识别为5,将3识别为8,将8识别为8。

除这些错误外,Oct读取为0c:或0 ::,星期三显示为男性

使用的脚本:

config= "-c tessedit_char_whitelist=01234567890.:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz -psm 3 -oem 0"
text = pytesseract.image_to_string(image, config=config)

还尝试使用1-12之间的不同psm编号,但没有运气。

使用其他脚本会增加对比度,导致更糟的情况是无法识别更多数字:

kernel = np.ones((2,2),np.uint8)
dilation = cv2.dilate(im, kernel)#,iterations = 1)
text = pytesseract.image_to_string(dilation, config=config)

原始数据 How the raw data looks like

运行脚本后 After running the script

运行新脚本后 After running new script

1 个答案:

答案 0 :(得分:2)

一些在将图像丢入Pytesseract之前进行清洁/平滑的预处理可能会有所帮助。具体而言,关闭小孔并消除噪声的形态学操作可以增强图像。另外,应用锐化滤镜也可能会有所帮助。调整内核大小或类型也可能有所帮助。我相信--psm 6是最好的选择,因为图像是一个统一的文本块。这是我在简单的变形关闭后得到的结果

enter image description here

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image = cv2.imread('1.png',0)
thresh = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY_INV)[1]

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
result = 255 - close

data = pytesseract.image_to_string(result, lang='eng',config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.imshow('close', close)
cv2.waitKey()
相关问题