我想从附加的图像(png 文件)中读取一列数字。
我的代码是
import cv2
import pytesseract
import os
img = cv2.imread(os.path.join(image_path, image_name), 0)
config= "-c
tessedit_char_whitelist=01234567890.:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
pytesseract.image_to_string(img, config=config)
这段代码给了我输出字符串:'n113\nun\n1.08'。正如我们所见,有两个问题:
这些问题的解决方案是什么?
最好的
最佳答案
您需要对图像进行预处理。一种简单的方法是调整图像大小,转换为灰度,然后使用 Otsu 阈值获得二值图像。从这里我们可以应用轻微的高斯模糊,然后反转图像,以便提取的所需文本为白色,背景为黑色。这是准备好用于 OCR 的处理过的图像
OCR 的结果
1.13
1.11
1.08
代码
import cv2
import pytesseract
import imutils
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
# Resize, grayscale, Otsu's threshold
image = cv2.imread('1.png')
image = imutils.resize(image, width=400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Blur and perform text extraction
thresh = 255 - cv2.GaussianBlur(thresh, (5,5), 0)
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)
cv2.imshow('thresh', thresh)
cv2.waitKey()
关于python - 使用 Tesseract OCR 从表格图像中识别特定数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60023800/