【opencv】信用卡号识别实验

2024-05-13 23:52
文章标签 opencv 实验 识别 信用卡

本文主要是介绍【opencv】信用卡号识别实验,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实验环境:anaconda、jupyter notebook(其它的ide也行)

实验用的包:numpy、matplotlib、opencv

实验目标:

识别信用卡的卡号

信用卡图片:

数字模板图片:

一、包引入

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

二、数字模板特征提取

图片二值化处理

template = cv2.imread('template.png')# 灰度处理
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
# 二值处理
ret,template_bin = cv2.threshold(template_gray,127,255,cv2.THRESH_BINARY_INV)plt.imshow(template_bin, 'gray')
plt.show()

数字模板二值化处理.png

检测数字模板外轮廓

# 只检测外轮廓
binary, template_contours, hierarchy = cv2.findContours(template_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
template_copy = template.copy()template_contours_img = cv2.drawContours(template_copy, template_contours, -1, (0,0,255),1)plt.imshow(template_contours_img)
plt.show()

数字模板外轮廓.png

找到外接矩形

# 外接矩形
tangles = []
template_copy = template.copy()
for cnt in template_contours:x,y,w,h = cv2.boundingRect(cnt)tangles.append((x,y,w,h))template_copy = cv2.rectangle(template_copy, (x, y), (x + w, y + h), (0,255,0), 2)
plt.imshow(cv2.cvtColor(template_copy, cv2.COLOR_BGR2RGB))
plt.show()
# 根据x值狠狠排序
tangles.sort()
print(tangles)

数字模板外接矩形.png

通过外接矩形截取数字图片

# 拿到数字对应的图片
number_size = (50,100)
digits = {}
for i in range(len(tangles)):(x,y,w,h) = tangles[i]digits[i] = cv2.resize(template_bin[y:y+h, x: x+w], number_size)plt.subplot(1,10,1 + i)plt.xticks([])plt.yticks([])plt.imshow(digits[i],'gray')
plt.show()

数字模板.png

三、信用卡卡面特征提取

初始化卷积核、处理卡片

# 初始化卷积核
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT,(9,3))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))# 读入信用卡图片
card = cv2.imread('card.png')
# 处理为灰度图
card_gray = cv2.cvtColor(card, cv2.COLOR_BGR2GRAY)
# 处理为礼帽处理
card_tophat = cv2.morphologyEx(card_gray, cv2.MORPH_TOPHAT, rectKernel)plt.imshow(card_tophat,'gray')
plt.show()

信用卡礼帽.png

对信用卡图片梯度处理

gradx = cv2.Sobel(card_tophat, ddepth=cv2.CV_32F, dx=1,dy=0, ksize=-1)
card_gradx = np.absolute(gradx)
(min_val,max_val) = (np.min(card_gradx), np.max(card_gradx))
card_gradx = (255 * ((card_gradx - min_val) / (max_val - min_val)))
card_gradx = card_gradx.astype('uint8')plt.imshow(card_gradx,'gray')
plt.show()

信用卡梯度处理.png

把特征轮廓合成一片,方便提取

闭操作

card_closing = cv2.morphologyEx(card_gradx, cv2.MORPH_CLOSE,rectKernel)plt.imshow(card_closing,'gray')
plt.show()

信用卡闭操作.png

二值化

# 二值化,自动判断
ret,card_bin = cv2.threshold(card_closing,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)plt.imshow(card_bin,'gray')
plt.show()

信用卡二值化处理.png

膨胀操作

# 膨胀操作,加强卡号特征
card_dilate = cv2.dilate(card_bin, rectKernel, iterations=1)plt.imshow(card_dilate,'gray')
plt.show()

信用卡膨胀.png

获取外轮廓

# 检测外轮廓
binary, card_contours, hierarchy = cv2.findContours(card_dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
card_copy = card.copy()card_contours_img = cv2.drawContours(card_copy, card_contours, -1, (0,0,255),2)plt.imshow(cv2.cvtColor(card_contours_img,cv2.COLOR_BGR2RGB))
plt.show()

此时拿到的外轮廓有很多不需要的地方

信用卡外轮廓.png

获取需要的外轮廓

card_copy = card.copy()
locs = []
for (i, sit) in enumerate(card_contours):(x,y,w,h) = cv2.boundingRect(sit)ar = w / float(h)if ar > 2.5 and ar < 4.0:if 65 < w < 70 and 15 < h < 25:locs.append((x,y,w,h))
locs.sort()
for (x,y,w,h) in locs:card_copy = cv2.rectangle(card_copy, (x, y), (x + w, y + h), (0,255,0), 2)plt.imshow(cv2.cvtColor(card_copy, cv2.COLOR_BGR2RGB))
plt.show()

信用卡卡号外轮廓提取.png

四、卡号识别

output = []
card_copy = card.copy()
for (i,(x,y,w,h)) in enumerate(locs):group_output = []# 获取每组卡号group = card_gray[y - 5 : y + h + 5, x - 5 : x + w + 5]# 转为二值图像ret, group = cv2.threshold(group,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)# 外轮廓检测binary, group_contours, hierarchy = cv2.findContours(group, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)data_sites = []for data_sit in group_contours:data_sites.append(cv2.boundingRect(data_sit))data_sites.sort()for (gx, gy, gw, gh) in data_sites:data = cv2.resize(group[gy : gy + gh, gx : gx + gw], number_size)scores = []# 计算分数for (digit, digit_img) in digits.items():result = cv2.matchTemplate(data, digit_img, cv2.TM_CCOEFF_NORMED)(_,score,_,_) = cv2.minMaxLoc(result)scores.append(score)group_output.append(str(np.argmax(scores)))cv2.rectangle(card_copy,(x - 5, y - 5), (x + w +5, y + h + 5), (0,255,0),2)cv2.putText(card_copy, "".join(group_output), (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0,0,255),2)output.extend(group_output)plt.imshow(cv2.cvtColor(card_copy, cv2.COLOR_BGR2RGB))
plt.show()
print(output)

成功识别卡号

信用卡卡号识别.png

这篇关于【opencv】信用卡号识别实验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/987163

相关文章

vcpkg安装opencv中的特殊问题记录(无法找到opencv_corexd.dll)

我是按照网上的vcpkg安装opencv方法进行的(比如这篇:从0开始在visual studio上安装opencv(超详细,针对小白)),但是中间出现了一些别人没有遇到的问题,虽然原因没有找到,但是本人给出一些暂时的解决办法: 问题1: 我在安装库命令行使用的是 .\vcpkg.exe install opencv 我的电脑是x64,vcpkg在这条命令后默认下载的也是opencv2:x6

基于CTPN(tensorflow)+CRNN(pytorch)+CTC的不定长文本检测和识别

转发来源:https://swift.ctolib.com/ooooverflow-chinese-ocr.html chinese-ocr 基于CTPN(tensorflow)+CRNN(pytorch)+CTC的不定长文本检测和识别 环境部署 sh setup.sh 使用环境: python 3.6 + tensorflow 1.10 +pytorch 0.4.1 注:CPU环境

百度OCR识别结构结构化处理视频

https://edu.csdn.net/course/detail/10506

brew install opencv@2 时报错 Error: Can't create update lock in /usr/local/var/homebrew/locks!

解决方案,报错里已经说明了: 我的解决方案: sudo chown -R "$USER":admin /usr/local   stackoverflow上的答案 I was able to solve the problem by using chown on the folder: sudo chown -R "$USER":admin /usr/local Also you'

Pycharm配置conda环境(解决新版本无法识别可执行文件问题)

引言: 很多小伙伴在下载最新版本的pycharm或者更新到最新版本后为项目配置conda环境的时候,发现文件夹目录中无法显示可执行文件(一般为python.exe),以下就是本人遇到该问题后试验和解决该问题的一些方法和思路。 一般遇到该问题的人群有两种,一种是刚入门对pycharm进行conda环境配置的小白(例如我),不熟悉相关环境配置的操作和过程,还有一种是入坑pycharm有段时间的老手

神经网络第四篇:推理处理之手写数字识别

到目前为止,我们已经介绍完了神经网络的基本结构,现在用一个图像识别示例对前面的知识作整体的总结。本专题知识点如下: MNIST数据集图像数据转图像神经网络的推理处理批处理  MNIST数据集          mnist数据图像 MNIST数据集由0到9的数字图像构成。像素取值在0到255之间。每个图像数据都相应地标有“7”、“2”、“1”等数字标签。MNIST数据集中,

高性能并行计算华为云实验五:

目录 一、实验目的 二、实验说明 三、实验过程 3.1 创建PageRank源码 3.2 makefile的创建和编译 3.3 主机配置文件建立与运行监测 四、实验结果与分析 4.1 采用默认的节点数量及迭代次数进行测试 4.2 分析并行化下节点数量与耗时的变化规律 4.3 分析迭代次数与耗时的变化规律 五、实验思考与总结 5.1 实验思考 5.2 实验总结 E

vscode python pip : 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称

在vscode中控制台运行python文件出现:无法将"pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。 使用vscode开发python,需要安装python开发扩展: 本文已经安装,我们需要找的是python安装所在目录,本文实际路径如下: 如果在本文路径中没有此目录,请尝试在C盘中搜索 python,搜索到相关python目录后,点击Python 3.9进入目录,

《学习OpenCV》课后习题解答7

题目:(P105) 创建一个结构,结构中包含一个整数,一个CvPoint和一个 CvRect;称结构体为“my_struct”。 a. 写两个函数:void Write_my_strct(CvFileStorage* fs, const char * name, my_struct* ms) 和 void read_my_struct(CvFileStorage* fs, CvFileNode

OpenCV中的按钮问题

在HighGUI中,没有显示提供任何形式的按钮。一般有两种方法替代: 1.用只有两个状态的滑动条来替代按钮。开关(switch)事实上就是只有两个状态的滑动条,这两个状态是on和off。然后通过回调函数来实现相关的功能。 实例源码(使用滑动条实现一个开关功能) #include<cv.h>#include<highgui.h>int g_switch_value = 0;void swit