信用卡数字识别

刚刚学了信用卡数字识别的案例,在此记录一下。。用的是模板匹配的方法

首先是对模板的处理,这是所用的模板,主要的任务是将这是十个数字进行分割标号,即第一个位置标识为0,第二个位置标识为2…
先对该模板图片进行轮廓检测,然后画出每一个数字的外接矩形,再对这十个外接矩形进行排序,得到相应的字典,每个位置对应相应的数字。
在这里插入图片描述

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 = myutils.sort_contours(refCnts, method="left-to-right")[0] #排序,从左到右,从上到下
digits = {}
# 遍历每一个轮廓
for (i, c) in enumerate(refCnts):   #枚举
	# 计算外接矩形并且resize成合适大小
	(x, y, w, h) = cv2.boundingRect(c)
	roi = ref[y:y + h, x:x + w]
	roi = cv2.resize(roi, (57, 88))
	# 每一个数字对应每一个模板
	digits[i] = roi

然后再对识别的图像进行处理
可以用一系列的图片变换操作来得到信用卡上含有卡号的区域。这里主要用的方法是礼帽操作和开闭运算,然后再进行区域的筛选,可以用横纵比的方法,选出我们感兴趣的区域。

	# 选择合适的区域,根据实际任务来,这里的基本都是四个数字一组
	if ar > 2.5 and ar < 4.0:

		if (w > 40 and w < 55) and (h > 10 and h < 20):
			#符合的留下来
			locs.append((x, y, w, h))

在这里插入图片描述
得到卡号的区域后,我们可以获得四组数据,再将每一组数据进行分割提取,来得到每个数字。
在这里插入图片描述
最后进行模板匹配,计算匹配得分,返回分数最高的数字

		# 在模板中计算每一个得分
		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)))

结果展示

from imutils import contours import numpy as np import argparse import cv2 as cv import myutils def cv_show(name,img): cv.imshow(name,img) cv.waitKey(0) cv.destroyAllWindows() # 先处理template tempalte_img = cv.imread("E:/opencv/picture/ocr_a_reference.png") tempalte_gray = cv.cvtColor(tempalte_img, cv.COLOR_BGR2GRAY) tempalte_thres = cv.threshold(tempalte_gray, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY_INV)[1] temp_a, tempalte_contours, temp_b = cv.findContours(tempalte_thres.copy (), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) cv.drawContours(tempalte_img, tempalte_contours, -1, (0, 255, 0), 2) tempalte_contours = contours.sort_contours(tempalte_contours, method="left-to-right")[0] digits = {} # 构建一个字典 for (i, c) in enumerate(tempalte_contours): (x, y, w, h) = cv.boundingRect(c) tempalte_roi = tempalte_thres[y:y + h, x:x + w] #之前一直检测不出正确答案,原因是这里的roi应该是tempalte_thres一部分 #而不是template_gray的一部分! tempalte_roi = cv.resize(tempalte_roi, (57, 88)) digits[i] = tempalte_roi cv_show('template_single',tempalte_roi) #cv_show('template_single',tempalte_roi) #对银行卡进行处理,之所以要做成数字长条,是想通过长条的尺寸比例大小来将自己想要的数字给抠出来。 rectkernel = cv.getStructuringElement(cv.MORPH_RECT,(9,3)) squrkernel = cv.getStructuringElement(cv.MORPH_RECT,(5,5)) image = cv.imread("E:/opencv/picture/credit_card_02.png") image = myutils.resize(image, width=300) image_gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) image_tophat= cv.morphologyEx(image_gray,cv.MORPH_TOPHAT,rectkernel) image_close = cv.morphologyEx(image_tophat,cv.MORPH_CLOSE,rectkernel) cv.imshow("image_tophat",image_tophat) cv.imshow('image_close',image_close) image_thres= cv.threshold(image_close,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)[1] image_contours= cv.findContours(image_thres.copy(),cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)[1] locs = [] for(n,con) in enumerate(image_contours): (gx,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值