OpenCV—案例(数字验证码识别)

 只是做个笔记   案例为实现想要的效果  (#^.^#)

OpenCV—案例(数字验证码识别)_第1张图片OpenCV—案例(数字验证码识别)_第2张图片

import cv2 as cv
from PIL import Image
import pytesseract as tess


# opencv + Tesserct-ORC

def text():
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    
    # 去线
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (1, 2))
    bin1 = cv.morphologyEx(binary, cv.MORPH_RECT, kernel)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (2, 1))
    bin2 = cv.morphologyEx(binary, cv.MORPH_RECT, kernel)
    cv.imshow("binary", bin2)

    cv.bitwise_not(bin2, bin2)
    test_img = Image.fromarray(bin2)
    
    # 识别图片中字
    test = tess.image_to_string(test_img)
    print("识别结果:%s"%test)

src = cv.imread("./../img/yanzheng/0.png")
cv.namedWindow("input img", cv.WINDOW_AUTOSIZE)
cv.imshow("input img", src)
text()
cv.waitKey(0)
cv.destroyAllWindows()

 

你可能感兴趣的:(OpenCV)