【发布时间】: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。
这是我的形象:
这是我的完整代码:
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