【问题标题】:Extract number from image with 2D array using Python tesseract使用 Python tesseract 从具有二维数组的图像中提取数字
【发布时间】:2018-05-28 10:25:50
【问题描述】:

在从包含 2D 矩阵形式的图像中提取整数期间,Tesseract 无法给出正确的结果,并且每次执行代码时结果都会有所不同,任何人都可以知道下面的代码中缺少什么

img = cv2.imread(img_path)
rows = img.shape[0]
cols = img.shape[1]

#print rows , cols

img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Apply dilation and erosion to remove some noise
#kernel = np.ones((5,5), np.uint64)
#img = cv2.dilate(img, kernel, iterations=1)
#img = cv2.erode(img, kernel, iterations=1)

# Write image after removed noise

cv2.imwrite(src_path + "removed_noise1.png", img)

#  Apply threshold to get image with only black and white
#img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,225,95)
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C , cv2.THRESH_BINARY ,251,95)
#print cv2.getGaussianKernel(ksize=221,sigma=41)

# Write the image after apply opencv to do some ...
cv2.imwrite(src_path + "thres1.png", img)

# Recognize text with tesseract for python  
result = pytesseract.image_to_string(Image.open(src_path + "thres1.png"))

输入

阈值:adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY ,251,95

阈值输出:

输出的形式为 1 5 5 7 5 7 3 8 6 4 9 0 2 4 8 6 1 3 0 2 3 9 0 8 9 可以是行主要形式和列主要形式无关紧要但我们确实需要将给定的输出保存到变量中

【问题讨论】:

  • 你能上传一些示例输入图像及其自适应阈值输出吗?
  • 我已附上输入文件
  • 是不是不小心又发了同一张图?
  • 预期的输出是什么?您在此图像中有一个网格,您希望输出哪种格式?
  • 输出格式为 1 5 5 7 5 7 3 8 6 4 9 0 2 4 8 6 1 3 0 2 3 9 0 8 9 可以是行主格式和列主格式形式无关紧要,但我们确实需要将给定的输出保存到变量中

标签: python opencv tensorflow tesseract


【解决方案1】:

尝试将阈值从 251、95 更改为 251、40 之类的值。

img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C , cv2.THRESH_BINARY ,251,40)

您似乎已经有了完美的图像,不需要任何更改即可从图像中提取字符串。 Pytesseract 的 image_to_string 在我的系统上不起作用,所以我使用了某人逐字制作的 OCR。这绝对不是世界上最好的解决方案,嘿,如果它有效,它就有效。我附上了一些文件(见下面的谷歌驱动器链接)

步骤:

  1. 运行 slice_img.py :这会将您的图像分成 5x5 块。
  2. 运行 TrainAndTest.py :这将从每个切片中提取数字 我们创建的图像并将其保存到 data.txt 文件中。

请注意:

  1. Training_chars.png、classifications.txt 和 flattened_images.txt 是 OCR 工作所必需的文件。
  2. 您必须为上述安装了 numpy、PIL、OpenCV 库 脚本工作
  3. 请将提供的所有文件保存在同一文件夹中,并使用图像名称作为 测试.png

TrainAndTest.py - https://drive.google.com/file/d/0B05aeuFExe2Aa3p3SWszN2xqU2c/view?usp=sharing
slice_image.py - https://drive.google.com/file/d/0B05aeuFExe2AN0t3UUlGZ3VjcW8/view?usp=sharing
training_chars.png - https://drive.google.com/file/d/0B05aeuFExe2ANjJNbzV5VTJyRTA/view?usp=sharing
classifications.txt - https://drive.google.com/file/d/0B05aeuFExe2AZU91bUpOblB3d2c/view?usp=sharing
flattened_images.txt - https://drive.google.com/file/d/0B05aeuFExe2AeXVnbXVXVTZ2RTQ/view?usp=sharing

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多