我对原始图像进行了以下调整:
这会产生下图
使用超正方体,我将图像转换为字符串,但它似乎只能识别 4。
转换为文本的代码 -
print (tess.image_to_string(img, config='--psm 6 -c tessedit_char_whitelist="9876543210"'))
4
然后,我尝试使用以下代码进行锐化,得到下一张图像,但 tesseract 仍然只能识别 4。知道如何进一步锐化,以便 tesseract 将其识别为 40 吗?
kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
sharpened = cv2.filter2D(img,-1,kernel)
print (tess.image_to_string(sharpened, config='--psm 6 -c tessedit_char_whitelist="9876543210"'))
4
或者,原始图像如下,没有任何调整大小。
Tesseract 确实将其拾取为 40,但我需要它来拾取更大的图像。有没有办法可以调整大小但保持质量/清晰度?
调整代码大小 -
img = cv2.resize(img,(0,0),fx=5,fy=5)
如果您有可能使用 ImageMagic:
import subprocess
import cv2
import pytesseract
# Image manipulation
# Commands https://imagemagick.org/script/convert.php
mag_img = r'D:\Programme\ImageMagic\magick.exe'
con_bw = r"D:\Programme\ImageMagic\convert.exe"
in_file = r'40.png'
out_file = r'40_bw.png'
# Play with black and white and contrast for better results
process = subprocess.run([con_bw, in_file, "-resize", "100%","-threshold","60%", out_file])
# Text ptocessing
pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = cv2.imread(out_file)
# Parameters see tesseract doc
custom_config = r'--psm 7 --oem 3 -c tessedit_char_whitelist=01234567890'
tex = pytesseract.image_to_string(img, config=custom_config)
print(tex)
with open("number.txt", 'w') as f:
f.writelines(tex)
cv2.imshow('image',img)
cv2.waitKey(12000)
cv2.destroyAllWindows()