簡體   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