我想從驗證碼影像中提取數字,所以我從這個答案
outPut: 436359 
outPut:
編輯:
我嘗試了
uj5u.com熱心網友回復:
通常,在像這樣的影像上獲得恰到好處的 OCR 與轉換的順序和引數有關。例如,在下面的代碼片段中,我首先轉換為灰度,然后腐蝕像素,然后膨脹,然后再次腐蝕。我使用閾值轉換為二進制(只是黑色和白色),然后再膨脹和腐蝕一次。這對我來說產生了正確的 859917 值并且應該是可重現的。
import cv2
import numpy as np
import pytesseract
file = 'sample2.jpg'
img = cv2.imread(file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ekernel = np.ones((1,2),np.uint8)
eroded = cv2.erode(gray, ekernel, iterations = 1)
dkernel = np.ones((2,3),np.uint8)
dilated_once = cv2.dilate(eroded, dkernel, iterations = 1)
ekernel = np.ones((2,2),np.uint8)
dilated_twice = cv2.erode(dilated_once, ekernel, iterations = 1)
th, threshed = cv2.threshold(dilated_twice, 200, 255, cv2.THRESH_BINARY)
dkernel = np.ones((2,2),np.uint8)
threshed_dilated = cv2.dilate(threshed, dkernel, iterations = 1)
ekernel = np.ones((2,2),np.uint8)
threshed_eroded = cv2.erode(threshed_dilated, ekernel, iterations = 1)
text = pytesseract.image_to_string(threshed_eroded)
print(''.join(x for x in text if x.isdigit()))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/345428.html
標籤:Python opencv 计算机视觉 蟒蛇成像库 验证码
