数字图像处理(图像分割--简单识别银行卡号)

不断学习中......

读取自己拍摄的银行卡号,并显示出来

通过高斯模糊以及设置阈值将图像二值化

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt


def image_process(file_path):
    img = cv.imread(file_path, 0)
    blur = cv.GaussianBlur(img, (9, 9), 0)     #高斯模糊

#设定阈值60到255,将图像二值化
    ret, binary = cv.threshold(blur, 60, 255, cv.THRESH_BINARY)       

    kernel = np.ones((2, 50), np.uint8)
    erosion = cv.erode(binary, kernel)         
    dilation = cv.dilate(erosion, kernel)      

    contours, hierarchy = cv.findContours(dilation, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    sp = dilation.shape
    for i in range(0, len(contours)):
        x, y, w, h = cv.boundingRect(contours[i])
        if h > sp[0]*0.05 and w > sp[1]*0.5 and y > sp[0]*0.2 and y < sp[0]*0.8 and w/h > 5:
            img = binary[y:y + h, x:x + w]
            cv.imshow("haoma", img)
            break

读取图像并且输出卡号:


src = cv.imread("./hm2.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input iamge",src)
image=cv.imread("./yhk.png")
image_process("./yhk1.png")
cv.waitKey(0)

cv.destroyAllWindows()

效果图如下:

数字图像处理(图像分割--简单识别银行卡号)_第1张图片

上面的一个图为识别过后的图,下面的为参考图,在拍摄银行卡的时候,可能因为拍摄出来太模糊而识别不出,可以多拍摄几张。 

你可能感兴趣的:(opencv,python)