【问题标题】:Extract digit from noisy image从噪声图像中提取数字
【发布时间】:2020-02-19 23:18:21
【问题描述】:

从噪声图像中提取数字

我想从手机相机拍摄的图像中提取文字。 首先,我尝试使用以下代码将图像转换为灰度:

imgg = Image.open('originale.jpg').convert('LA')

其次,我尝试使用此代码对灰度图像进行阈值处理以获取只有黑白的图像::

 retval, threshold = cv2.threshold(grayscaled, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imwrite("threshold.png", threshold)

第三次我尝试使用 pytesseract 提取文本,但我没有使用这段代码得到正确的结果。

result5 = pytesseract.image_to_string(Image.open("threshold.png"))

这是我要提取数字的图像,例如: 我的预期输出是:111 2 11 4 1 23 2 3

这是我的形象:

originale.jpg

threshold.png

这是我的完整代码:

import cv2
import numpy as np
import pytesseract
from PIL import Image
img = cv2.imread('originale.jpg')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(grayscaled, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imwrite("threshold.png", threshold)
result = pytesseract.image_to_string(Image.open("threshold.png"))
print(result)

【问题讨论】:

  • 我建议您将图像转换为灰度并对其进行阈值处理,以便在白色背景上有黑色数字。然后尝试将数字提取为文本。
  • 我尝试像这样imgg = Image.open('6.jpg').convert('LA') imgg.save('greyscale.png') 将我的图像转换为灰度,然后我尝试对图像进行阈值处理retval, threshold = cv2.threshold(imgg, 49, 255, cv2.THRESH_BINARY)

标签: python image opencv cv2 python-tesseract


【解决方案1】:

您可以使用 Otsu 方法来确定最佳阈值以精确您的数字。

import cv2

img # this is your original image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imwrite("threshold.png", threshold)

结果:

【讨论】:

  • 我试试这个代码,但我什么都没有:import cv2 import numpy as np import pytesseract from PIL import Image img = cv2.imread('3.jpg') grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) retval, threshold = cv2.threshold(grayscaled, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) cv2.imwrite("threshold.png", threshold) result6 = pytesseract.image_to_string(Image.open("threshold.png")) print(result6)
猜你喜欢
  • 2020-05-29
  • 2023-04-05
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 2011-01-27
  • 2023-03-16
  • 2021-08-16
相关资源
最近更新 更多