Bootstrap

OCR模板匹配识别银行卡号

利用ocr实现模板匹配
ocr识别银行卡号

待识别银行卡模板图像
在这里插入图片描述

1. 思路

  1. 读取模板图像
  2. 处理模板图像,做成字典以待查询使用
  3. 读取待识别银行卡
  4. 礼貌运算突出线条
  5. sobel算子找边缘信息
  6. 闭运算,轮廓查找、过滤轮廓等
  7. 提取数字,模板匹配
  8. 显示

2.代码

需要的安装包:

  • imutils
  • numpy
  • opencv3.4
  • python3.6

主要代码:

from imutils import contours
import numpy as np
import imutils
import cv2

def ocr_card_match(tem_path,image_path):

    #读取模板图像,灰度转换,阈值处理
    img_ref = cv2.imread(tem_path)
    img_ref = cv2.cvtColor(img_ref,cv2.COLOR_BGR2GRAY)
    img_ref = cv2.threshold(img_ref,10,255,cv2.THRESH_BINARY_INV)[1]

    #显示测试
    # print(img_ref)
    # cv2.namedWindow("test",cv2.WINDOW_AUTOSIZE)
    # cv2.imshow("test", img_ref)
    # cv2.waitKey(0)

    #寻找轮廓
    imgCnts = cv2.findContours(img_ref.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    imgCnts = imutils.grab_contours(imgCnts)
    imgCnts = contours.sort_contours(imgCnts, method= "left-to-right")[0]
    digits ={}

    # 轮廓处理
    for (i, c) in enumerate(imgCnts):
        (x,y,w,h) = cv2.boundingRect(c)
        roi = img_ref[y:y+h, x:x+w]
        roi = cv2.resize(roi, (57,88))
        #更新字典
        digits[i] = roi

    #核函数构建
    rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,3))
    rectKernel5 = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))

    #读取待匹配图像
    img = cv2.imread(image_path)
    img = imutils.resize(img, width=300)
    img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    #礼貌运算突出线条
    tophat_img = cv2.morphologyEx(img_gray,cv2.MORPH_TOPHAT,rectKernel)

    #用sobel算子找边缘信息,ksize=-1表示sobel维度为(3,3)
    gradX = cv2.Sobel(tophat_img,ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
    #取绝对值
    gradX = np.absolute(gradX)
    (minVal,maxVal) = (np.min(gradX), np.max(gradX))
    gradX = (255 * ((gradX-minVal)/ (maxVal-minVal)))
    gradX = gradX.astype("uint8")

    #闭运算,先膨胀再腐蚀,数字连成一块
    gradX =cv2.morphologyEx(gradX,cv2.MORPH_CLOSE, rectKernel)
    #二值化
    thresh = cv2.threshold(gradX, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    #再闭操作,内部空白填充
    thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, rectKernel5)

    #轮廓查找
    cnts = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    locs = []

    #筛选一下轮廓
    for (i, c) in enumerate(cnts):
        (x, y, w, h) = cv2.boundingRect(c)
        ratio = w/float(h)

        if ratio>2.5 and ratio < 4.0:
            if(w>40 and w<55) and (h>10 and h<20):
                locs.append((x,y,w,h))

    #从左到右排序
    locs = sorted(locs,key=lambda x:x[0])

    output = []
    #遍历,提取其中数字,与模板数字匹配
    for (i, (gX, gY, gW, gH)) in enumerate(locs):
        groupOutput = []

        group = img_gray[gY -5 :gY +gH +5, gX -5:gX+gW +5]
        group = cv2.threshold(group, 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)[1]

        #轮廓查找,排序
        digitCnts = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        digitCnts =imutils.grab_contours(digitCnts)
        digitCnts = contours.sort_contours(digitCnts, method="left-to-right")[0]

        for c in digitCnts:
            (x, y, w, h) = cv2.boundingRect(c)
            roi = group[y:y + h, x:x + w]
            roi = cv2.resize(roi, (57, 88))

            scores = []
            #模板匹配
            for(digit, digitROI) in digits.items():

                result = cv2.matchTemplate(roi,digitROI, cv2.TM_CCOEFF)
                (_, score, _, _) = cv2.minMaxLoc(result)
                scores.append(score)
            groupOutput.append(str(np.argmax(scores)))

        cv2.rectangle(img,(gX-5, gY-5), (gX+gW+5, gY+gH+5), (0,0,255), 2)

        cv2.putText(img,"".join(groupOutput),(gX, gY - 15), cv2.FONT_HERSHEY_SIMPLEX,
                    0.65, (0,0,255),2)
        #print(groupOutput)
        output.extend(groupOutput)

    print("Credit Card Number: {}".format("".join(output)))
    cv2.imshow("result", img)
    cv2.waitKey(5000)

if __name__=="__main__":
    tem_path = "/home/ubuntu/work/ocr_card/reference.png"
    image_path="/home/ubuntu/work/ocr_card/05.png"
    ocr_card_match(tem_path,image_path)

3.测试

运行代码结果如下:

待识别图像识别结果
在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述

Reference:
https://www.cnblogs.com/yangyue-kai/p/11578293.html

;