繁体   English   中英

从噪声图像中提取数字

[英]Extract digit from noisy image

从噪声图像中提取数字

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

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"))

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

这是我的形象:

原创.jpg

阈值.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)

您可以使用 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)

结果: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM