我尝试使用OCR来提取数字。
开发环境由pycharm (Python版本3)运行。
我的问题是如何使用OCR提取数字。
图像如下所示:

在上面的图片中,我希望获得以下数字文本:
1 2 3
4 5 6 7
8 9 0怎样才能得到我想要的结果呢?
发布于 2019-11-07 20:43:23
有一系列的库可以实现这一点,这里有一个来自https://pypi.org/project/pytesseract/ https://github.com/madmaze/pytesseract的示例:
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
# If you don't have tesseract executable in your PATH, include the following:
pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
# Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
# Simple image to string
print(pytesseract.image_to_string(Image.open('test.png')))发布于 2019-11-08 10:49:47
你可以用Otsu的阈值来获得一个二值图像,然后提取每个数字。阈值处理后,我们得到如下结果

现在,我们迭代轮廓线并提取/保存每个ROI

现在,您可以应用所需的OCR工具来读取每个ROI上的文本
import cv2
image = cv2.imread('1.jpg', 0)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c, num in zip(cnts, range(len(cnts))):
x,y,w,h = cv2.boundingRect(c)
ROI = 255 - thresh[y:y+h, x:x+w]
cv2.imwrite('ROI_{}.png'.format(num), ROI)
cv2.imshow('thresh', 255 - thresh)
cv2.waitKey()https://stackoverflow.com/questions/58748707
复制相似问题