简体   繁体   English

从噪声图像中提取数字

[英]Extract digit from noisy image

Extract digit from noisy image从噪声图像中提取数字

I want to extract text from an image taken by mobile phone camera.我想从手机相机拍摄的图像中提取文本。 First I try to convert the image to greyscale by using this code:首先,我尝试使用以下代码将图像转换为灰度:

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

Second i try to threshold the grey image to get image with only black and white with this code::其次,我尝试使用此代码对灰度图像进行阈值处理以获取只有黑白的图像::

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

Third i try to extract text with pytesseract but i have not the correct result with this code.第三,我尝试使用 pytesseract 提取文本,但这段代码的结果不正确。

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

This is the image which I want to extract digits number for example: My expected output is: 111 2 11 4 1 23 2 3 .这是我要提取数字的图像,例如:我预期的 output 是: 111 2 11 4 1 23 2 3

and this is my image:这是我的形象:

originale.jpg原创.jpg

threshold.png阈值.png

And this is my full code:这是我的完整代码:

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)

You can use Otsu method to determine optimal threshold value to exact your digits.您可以使用 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)

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

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

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