目录
一、项目介绍
项目的主要目标是实现信用卡号码和类型的识别。通过图像处理技术,从信用卡图像中提取出卡号,将每个数字与模板数字进行比对,从而得出信用卡号码。并根据卡号的第一位数字判断信用卡的类型。
二、数据材料介绍
1、模板图片(1张)

2、需要处理的信用卡图片(5张)





三、实现过程
1、导入需要用到的库
python
import numpy as np
import argparse
import cv2
import myutils
其中myutils模块为自己编写的工具模块,里面包含了对轮廓进行排序的函数以及自动变换图片大小的函数,内容如下:
python
"""myutil.py"""
import cv2
# 排序函数
def sort_contours(cnts, method='left-to-right'):
# 初始化排序方向和索引
reverse = False
axis_index = 0 # 默认按 x 轴排序(从左到右或从右到左)
# 根据排序方法设置排序方向和索引
if method == 'right-to-left' or method == 'bottom-to-top':
reverse = True # 反向排序
if method == 'top-to-bottom' or method == 'bottom-to-top':
axis_index = 1 # 按 y 轴排序(从上到下或从下到上)
# 计算每个轮廓的边界框
bounding_boxes = [cv2.boundingRect(c) for c in cnts]
# 将轮廓和边界框组合在一起
combined = list(zip(cnts, bounding_boxes))
# 根据边界框的坐标进行排序
sorted_combined = sorted(combined, key=lambda x: x[1][axis_index], reverse=reverse)
# 解包排序后的轮廓和边界框
sorted_cnts = [item[0] for item in sorted_combined]
sorted_bounding_boxes = [item[1] for item in sorted_combined]
return sorted_cnts, sorted_bounding_boxes
# 变换图片大小的函数
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
dim = None
(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)
#参数interpolation指定了在图像大小调整过程中如何处理像素插值的方法。cv2.INTER_AREA具体意味着使用面积插值方法。
return resized
2、设置命令行参数
- --image为信用卡图片
- --template为模板图片
python
ap = argparse.ArgumentParser()
ap.add_argument('-i','--image',required=True,help='')
ap.add_argument('-t','--template',required=True,help='')
args = vars(ap.parse_args())
# 信用卡号码开头对应信用卡的类型
FIRST_NUMBER = {"3":"American Express",
"4":"Visa",
"5":"MasterCard",
"6":"Discover Card"}
# 定义显示图片函数
def cv_show(name, image):
cv2.imshow(name, image)
cv2.waitKey(0)
3、模板图像中数字的定位处理
-
读取模板图像(包含 0-9 的数字)。
-
对模板图像进行灰度化、二值化处理。
-
使用轮廓检测提取每个数字的轮廓,并将每个数字裁剪出来,保存为模板。
python
"""模板图像中数字的定位处理"""
# img为模板图像
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.THRESH_BINARY_INV)[1]
cv_show('ref',ref)
# 轮廓
refCnts = cv2.findContours(ref.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1]
cv2.drawContours(img,refCnts,-1,(0,0,255),2)
cv_show('img',img)
# 对轮廓进行从左到右排序
refCnts = myutils.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
cv_show('roi',roi)
print(len(digits))

4、信用卡图像处理
-
读取信用卡图像。
-
对信用卡图像进行灰度化、顶帽操作(去除背景)、闭操作(将数字连在一起)、自适应二值化等处理。
-
使用轮廓检测找到信用卡上的数字区域。
python
"""信用卡的图像处理"""
image = cv2.imread(args['image'])
cv_show('image',image)
# 变换图片大小
image = myutils.resize(image,width=300)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv_show('gray',gray)
# 设置核
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT,(9,3))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# 顶帽
tophat = cv2.morphologyEx(gray,cv2.MORPH_TOPHAT,rectKernel)
# 开运算
open = cv2.morphologyEx(gray,cv2.MORPH_OPEN,rectKernel)
cv_show('open',open)
cv_show('tophat',tophat)
# 找数字边框
# 闭操作,将数字连在一起
closeX = cv2.morphologyEx(tophat,cv2.MORPH_CLOSE,rectKernel)
cv_show('closeX',closeX)
# 自适应二值化
thresh = cv2.threshold(closeX,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cv_show('thresh',thresh)
# 闭操作
thresh = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,sqKernel)
cv_show('thresh1',thresh)
# 计算轮廓
threshCnts = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1]
cnts = threshCnts
cur_img = image.copy()
cv2.drawContours(cur_img,cnts,-1,(0,0,255),3)
cv_show('img',cur_img)
# 遍历轮廓,找到数字部分
locs = [] # 存放每组数字的x,y,w,h
for (i,c) in enumerate(cnts):
(x,y,w,h) = cv2.boundingRect(c)
ar = w/float(h)
if 2.5 < ar < 4.0:
if (40 < w < 55) and (10 < h < 20):
locs.append((x,y,w,h))
locs = sorted(locs,key=lambda x: x[0])

5、模板匹配
-
将信用卡图像中的每个数字区域与模板中的数字进行匹配,找到最相似的数字。
-
根据匹配结果识别信用卡号码。
python
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_BINARY | cv2.THRESH_OTSU)[1]
cv_show("group",group)
# 寻找每组数字的轮廓并根据顺序放入digitCnts
digitCnts = cv2.findContours(group.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1]
digitCnts = myutils.sort_contours(digitCnts)[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)
# minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(src, mask=None)
score = cv2.minMaxLoc(result)[1]
scores.append(score)
# 得到匹配分数最大值的索引
groupOutput.append(str(np.argmax(scores)))
cv2.rectangle(image,(gx-5,gy-5),(gx+gw+5,gy+gh+5),(0,0,255),1)
cv2.putText(image,"".join(groupOutput),(gx,gy-15),cv2.FONT_HERSHEY_SIMPLEX,0.65,(0,255,0),2)
output.extend(groupOutput)
# 打印结果
print("信用卡类型:{}".format(FIRST_NUMBER[output[0]]))
print("信用卡号码:{}".format("".join(output)))
cv_show("Image",image)


四、总结
这个项目通过图像处理和模板匹配技术,实现了信用卡号码的自动识别。它展示了如何结合 OpenCV 和 Python 实现一个实用的图像处理应用。