对银行卡进行数字识别流程:
-
在模板中准备数字0-9;
-
对模板以及输入图像中的数字进行外轮廓检测;
-
对当前轮廓做外接矩形;
-
将输入图像转换为灰度图像,二值处理等等;
-
对检测到的多个轮廓进行过滤操作,可以利用固定的长宽比,过滤掉不符合的轮廓;
-
检测四个数字的大致轮廓,再将其切分成四个小区域;
-
对每个小区域进行模板匹配,输出结果。
代码:
# 导入工具包
from imutils import contours
import numpy as np
import argparse
import cv2
import myutils
# 设置参数
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-t", "--template", required=True,
help="path to template OCR-A image")
args = vars(ap.parse_args())
# 指定信用卡类型
FIRST_NUMBER = {
"3": "American Express",
"4": "Visa",
"5": "MasterCard",
"6": "Discover Card"
}
# 绘图展示
def cv_show(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 读取一个模板图像
img = cv2.imread(args["template"])
cv_show('img', img)
# 灰度图
ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv_show('ref', ref)
# 二值图像
ref = cv2.threshold(ref, 10, 255, cv2.