我有一些图片,我想从中读取数字。我使用pytesseract以及cv2阈值。
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
crop = ['crop.png','crop1.png','crop2.png','crop3.png']
for c in crop:
image = cv2.imread(c, 0)
#thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
thresh = cv2.threshold(image, 0, 255,cv2.THRESH_OTSU)[1]
#thresh = cv2.GaussianBlur(thresh, (1,3), 0 )
#thresh = cv2.adaptiveThreshold(thresh,125, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 12)
#thresh = cv2.bilateralFilter(thresh, 15, 80, 80, cv2.BORDER_DEFAULT)
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)
print('\nnext')
cv2.imshow('thresh', thresh)
但未获得良好的输出请告诉我我在哪里做错了。
这里是图片
您可以尝试在识别之前进行图像处理。例如,像这样:
image = cv2.imread(c, 0)
se=cv2.getStructuringElement(cv2.MORPH_ELLIPSE , (5,5))
close=cv2.morphologyEx(image, cv2.MORPH_CLOSE, se)
close=cv2.absdiff(close, image)
image=cv2.bitwise_not(close)
也尝试对图像进行升采样。希望这能解决您的问题。