环境介绍

python 3.6 + OpenCV 3.4.1.15

原理介绍

首先,提取出模板中每一个数字的轮廓,再对信用卡图像进行处理,提取其中的数字部分,将该部分数字与模板进行匹配,即可得到结果。

模板展示

完整代码 # !/usr/bin/env python

# —*— coding: utf-8 —*—

# @Time: 2020/1/11 14:57

# @Author: Martin

# @File: utils.py

# @Software:PyCharm

import cv2

def sort_contours(cnts, method='left-to-right'):

reverse = False

i = 0

if method == 'right-to-left' or method == 'bottom-to-top':

reverse = True

if method == 'top-to-bottom' or method == 'bottom-to-top':

i = 1

boundingboxes = [cv2.boundingRect(c) for c in cnts]

(cnts, boundingboxes) = zip(*sorted(zip(cnts, boundingboxes), key=lambda b: b[1][i], reverse=reverse))

return cnts, boundingboxes

def resize(image, width=None, height=None, inter=cv2.INTER_AREA):

(h, w) = image.shape[:2]

if width is None and height is None:

return image

if width is None:

r = height / float(h)

dim = (int(w * r), height)

else:

r = width / float(w)

dim = (width, int(h * r))

resized = cv2.resize(image, dim, interpolation=inter)

return resized

# !/usr/bin/env python

# —*— coding: utf-8 —*—

# @Time: 2020/1/11 14:57

# @Author: Martin

# @File: template_match.py

# @Software:PyCharm

"""

基于模板匹配的信用卡数字识别

"""

import cv2

import utils

import numpy as np

# 指定信用卡类型

FIRST_NUMBER = {

'3' : 'American Express',

'4' : 'Visa',

'5' : 'MasterCard',

'6' : 'Discover Card'

}

# 绘图显示

def cv_show(name, image):

cv2.imshow(name, image)

cv2.waitKey(0)

cv2.destroyAllWindows()

# 读取模板图像

img = cv2.imread('./images/ocr_a_reference.png')

cv_show('img', img)

# 转化成灰度图

ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv_show('ref', ref)

# 转化成二值图像

ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]

cv_show('ref', ref)

# 计算轮廓

ref_, refCnts, hierarchy = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(img, refCnts, -1, (0, 0, 255), 3)

cv_show('img', img)

print(np.array(refCnts).shape)

# 排序,从左到右,从上到下

refCnts = utils.sort_contours(refCnts, method='left-to-right')[0]

digits = {}

# 遍历每一个轮廓

for (i, c) in enumerate(refCnts):

(x, y , w, h) = cv2.boundingRect(c)

roi = ref[y:y+h, x:x+w]

roi = cv2.resize(roi, (57, 88))

digits[i] = roi

# 初始化卷积核

rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3))

sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))

# 读取输入图像,预处理

img_path = input("Input the path and image name: ")

image_input = cv2.imread(img_path)

cv_show('image', image_input)

image_input = utils.resize(image_input, width=300)

gray = cv2.cvtColor(image_input, cv2.COLOR_BGR2GRAY)

cv_show('gray', gray)

# 礼帽操作,突出更明亮的区域

tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)

cv_show('tophat', tophat)

gradX = cv2.Sobel(tophat, 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")

print(np.array(gradX).shape)

cv_show('gradX', gradX)

# 闭操作

gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)

cv_show('gradX', gradX)

thresh = cv2.threshold(gradX, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

cv_show('thresh', thresh)

thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)

cv_show('thresh', thresh)

# 计算轮廓

thresh_, threshCnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cnts = threshCnts

cur_img = image_input.copy()

cv2.drawContours(cur_img, cnts, -1, (0, 0, 255), 3)

cv_show('img', cur_img)

locs = []

# 遍历轮廓

for (i, c) in enumerate(cnts):

(x, y, w, h) = cv2.boundingRect(c)

ar = w / float(h)

if 2.5 < ar < 4.0 and (40 < w < 55) and (10 < h < 20):

locs.append((x, y, w, h))

# 将符合的轮廓从左到右排序

locs = sorted(locs, key=lambda ix: ix[0])

output = []

# 遍历每一个轮廓中的数字

for (i, (gX, gY, gW, gH)) in enumerate(locs):

groupOutput = []

group = gray[gY - 5:gY + gH + 5, gX - 5: gX + gW + 5]

cv_show('group', group)

# 预处理

group = cv2.threshold(group, 0, 255, cv2.THRESH_OTSU)[1]

cv_show('group', group)

# 计算每一组轮廓

group_, digitCnts, hierarchy = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

digitCnts = utils.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))

cv_show('roi', roi)

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(image_input, (gX - 5, gY - 5), (gX + gW + 5, gY + gH + 5), (0, 0, 255), 1)

cv2.putText(image_input, "".join(groupOutput), (gX, gY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)

# 得到结果

output.extend(groupOutput)

# 打印结果

print("Credit Card Type: {}".format(FIRST_NUMBER[output[0]]))

print("Credit Card #: {}".format("".join(output)))

cv2.imshow("Image", image_input)

cv2.waitKey(0)

cv2.destroyAllWindows()

结果展示

Credit Card Type: Visa

Credit Card #: 4020340002345678

总结

以上所述是小编给大家介绍的Python开发之基于模板匹配的信用卡数字识别功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对聚米学院网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

python信用卡客户_Python开发之基于模板匹配的信用卡数字识别功能相关推荐

  1. 基于模板匹配的0-9数字语音识别(matlab)

     一.通过提取语音的MFCC参数,与提前制作好的语音模板进行DTW匹配,实现0-9数字语音识别,且识别率达到一定要求,可以区分0-9中数字以及鉴别非0-9数字语音  二.对充足的模板进行聚类,找到聚类 ...

  2. 【数字识别】基于模板匹配实现OCR印刷字母+数字识别含Matlab源码

    1 简介 OCR技术是光学字符识别的缩写, 是通过扫描等光学输入方式将各种票据.报刊.书籍.文稿及其它印刷品的文字转化为图像信息, 再利用文字识别技术将图像信息转化为可以使用的计算机输入技术.由于其应 ...

  3. 【印刷字符识别】基于matlab OCR印刷字母+数字识别【含Matlab源码 1861期】

    ⛄一.OCR简介 OCR技术是光学字符识别的缩写, 是通过扫描等光学输入方式将各种票据.报刊.书籍.文稿及其它印刷品的文字转化为图像信息, 再利用文字识别技术将图像信息转化为可以使用的计算机输入技术. ...

  4. python opencv数字识别_基于模板匹配的手写数字识别(python+opencv)

    智能计算课第一周的实验是做基于模板匹配的手写数字识别,光听见就很感兴趣,于是决定认真做做这个实验,本实验基于python3+opencv的python版本,所用到的知识都比较简单,基本上边学边做,技术 ...

  5. python手写数字识别教学_python实现基于SVM手写数字识别功能

    本文实例为大家分享了SVM手写数字识别功能的具体代码,供大家参考,具体内容如下 1.SVM手写数字识别 识别步骤: (1)样本图像的准备. (2)图像尺寸标准化:将图像大小都标准化为8*8大小. (3 ...

  6. 论文解读:ACL2021 NER | 基于模板的BART命名实体识别

    摘要:本文是对ACL2021 NER 基于模板的BART命名实体识别这一论文工作进行初步解读. 本文分享自华为云社区<ACL2021 NER | 基于模板的BART命名实体识别>,作者: ...

  7. Python相机自动采集图像,然后模板匹配、自动截取保存图片

    前言 通过python调用相机然后间隔一段时间进行拍摄图片,进行识别提取. 提示:以下是本篇文章正文内容,下面案例可供参考 一.确保电脑存在相机等硬件设备 我的电脑没有相机硬件设备,于是我通过手机在局 ...

  8. 基于模板匹配的知识图谱问答系统

    基于模板匹配的知识图谱问答系统 一,什么是基于知识图谱的问答(KBQA)? KBQA–knowledge base question answering,通过对问题进行语义理解及解析,通过知识库进行查 ...

  9. 基于模板匹配和遗传算法的人眼定位

    基于模板匹配和遗传算法的人眼定位 余甜甜,唐普英(电子科技大学光电信息学院,四川成都6l0054) 摘要:文中提出了一种利用模板匹配与遗传算法相结合的人眼定位算法.根据人脸几何特征将人脸分为几个特征区 ...

最新文章

  1. linux服务器加固的命令,Linux 服务器安全加固
  2. 《The Art of Readable Code》 读书笔记 01
  3. 华为南太无线解决方案部梁旭阳_工业互联网产业联盟网络组走进华为南京研究所技术研讨会顺利召开...
  4. 多协程实例讲解(四 Python)
  5. 【Python入门】一个有意思还有用的Python包-汉字转换拼音
  6. ffempge 效率 生成_ffmpeg 编译静态库,搞了好久,终于搞成功了.根据网上教程做的 · iodefog...
  7. 让用户研究插上数据的翅膀
  8. python中3个while循环_python的input和while循环
  9. A[1080]Graduate Admission 两个cmp比较函数两个struct结构体
  10. abc物动量分析计算机视频,物动量ABC分类法
  11. python爬取雪球网交易数据
  12. Python 中文数字转英文阿拉伯数字
  13. CAN 错误帧和原理
  14. 什么是 NullPointerException?
  15. 数据分析学习技能树 | 养成数据分析师的品质和思维模式
  16. 数字技术战略:开发者体验 —— 内部工具的“最后一公里”
  17. php 简转繁体,PHP将简体汉字转为繁体的方法
  18. PEG/蛋白Prote/抗体antibody/PAA/SiO2功能化 修饰NaY(Gd/Lu/Nd):Yb,Tm@NaYF4:Yb,Nd上转换纳米颗粒
  19. 11种dialogBox样式打包开源,逐一详解
  20. spring boot 集成框架事例

热门文章

  1. 博弈论(一):Nim游戏
  2. 洛谷2661 信息传递 三倍经验?
  3. HDU 5928 DP 凸包graham
  4. jQuery分析(3) - jQuery.fn.init
  5. BZOJ 1013: [JSOI2008]球形空间产生器sphere( 高斯消元 )
  6. [转载]与索引相关的概念---聚簇因子
  7. C/C++指针 引用
  8. 大数据之-Hadoop3.x_MapReduce_序列化案例FlowBean---大数据之hadoop3.x工作笔记0097
  9. mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
  10. AndroidStudio_开发工具调试入门---Android原生开发工作笔记70