经历上次的失败后,这一次我学聪明了。
我自己做不出东西乱折腾啥也不是,我跟着别人,啃别人的代码,借鉴一下别人的它不香吗。
所以这次是用python做银行卡号码识别。
话不多说,开始!
import cv2
import numpy as np
# 定义绘图函数
def imshow(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imshow()画个图还要一套组合拳,接化发,咱年轻人顶不住,先给个函数打包定义一下。
# 读取模板图片
# 0表示读成一个单通道的图片 即灰度图 也可以用
# ref = cv2.cvtColor(img_num, cv2.COLOR_BGR2GRAY)转换成灰度图
img_num = cv2.imread('images/ocr_a_reference.png',0)
imshow('img_num',img_num)
- 转换成二值图像
# cv2.threshold(输入图像,阈值,赋值,方法) 这里方法是高于阈值取0,低于阈值取255
# cv2.threshold返回两个值 第二个值是我需要的处理后的图像
img_num_bin = cv2.threshold(img_num,10,255,cv2.THRESH_BINARY_INV)[1]
imshow('img_num_bin',img_num_bin)
因为接下来的银行卡数字会为白色部分,所以转成白字黑底,方便后续进行模板匹配
- 轮廓提取
# cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图),cv2.RETR_EXTERNAL只检测外轮廓,cv2.CHAIN_APPROX_SIMPLE只保留终点坐标
# 返回的list中每个元素都是图像中的一个轮廓
num_cnts_list, _ =cv2.findContours(img_num_bin.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img_num_bin, num_cnts_list, -1, (0,0,255), 3)
imshow('draw_img_num',img_num_bin)
轮廓没有显示出来,但是对比上面变窄了,是不是因为轮廓自动变成黑色的了。暂时不管,接下来要把每个轮廓圈起来,到时候看一看就知道了。
- 轮廓排序
num_rect_list = num_cnts_sort(num_cnts_list)
num_cnts_sort是我自定义的函数,用来给轮廓排序,并且按一定的顺序返回
def num_cnts_sort(list,right=1,up=0):
# up=1表示从上往下,right=1表示从左往右,-1表示反过来
reverse = False
if up==-1 or right== -1:
reverse = True
if up == 0:
# 左右方向排序 权重选x
i = 0
if right == 0:
i = 1
# 找到的轮廓用外接矩形框起来 cv2.boundingRect(c)返回x,y,w,h
boundingBoxs = [cv2.boundingRect(c) for c in list] #生成器
# sorted(输入序列,排序规则,reverse=True 由小到大否则由大到小)
# lambda 匿名函数 输入序列的每个元素 输出b[i]
boxs = sorted(boundingBoxs,key= lambda b: b[i],reverse=reverse )
return boxs
把每个轮廓外接矩形按顺序画出来
for num_rect in num_rect_list:
(x,y,w,h)=num_rect
num_rect_img = cv2.rectangle(img_num_bin.copy(),(x,y),(x+w,y+h),(255,0,0),2)
imshow('num_rect_img',num_rect_img)
排序没有问题,接下来把对应得数字模板截下来和数字对应
# 把图片和数字对应
num_rect_dic = {}
for (i,num_rect) in enumerate(num_rect_list):
(x, y, w, h) = num_rect
num_rect_item = img_num_bin[y:y+h,x:x+w]
num_rect_item = num_resize(num_rect_item,h_size=88)
# 把数字和截下来的图像对应
num_rect_dic[i]=num_rect_item
imshow('num_rect_item', num_rect_item)
num_resize为自定义函数,输入高或宽自动计算缩放
def num_resize(img,w_size=0,h_size=0):
(w,h)=img.shape # size返回总元素个数 和matlab不一样
if w_size == 0:
r = h_size/float(h)
w_size = int(r*h)
if h_size == 0:
r = w_size/float(w)
h_size = int(w*h)
resized = cv2.resize(img,(w_size,h_size))
return resized
- 对银行卡图像预处理
# 对银行卡图像预处理
# 读取图像
bank_img = cv2.imread('images/credit_card_01.png')
bank_img_gray = cv2.cvtColor(bank_img,cv2.COLOR_BGR2GRAY)
imshow('bank_img',bank_img)
imshow('bank_img_gray',bank_img_gray)
# 定义卷积核
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3)) # 矩形卷积核
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# 顶帽操作 突出明亮的部分
bank_img_tophat = cv2.morphologyEx(bank_img_gray,cv2.MORPH_TOPHAT, rectKernel)
imshow('bank_img_tophat',bank_img_tophat)
数字的横线太暗,我想 如果进行二值化要选择一个合适的参数,或者进行一个边缘检测。
- 边缘检测
# X方向边缘检测处理
bank_img_grad = cv2.Sobel(bank_img_tophat,cv2.CV_32F,1,0,ksize=-1)
imshow('bank_img_tophat',bank_img_grad)
# 归一化
bank_img_grad_abs = np.absolute(bank_img_grad)
(max,min) = (np.max(bank_img_grad_abs),np.min(bank_img_grad_abs))
bank_img_grad_abs = (255*(bank_img_grad_abs-min)/(max-min))
bank_img_grad_abs = bank_img_grad_abs.astype('uint8')
imshow('s',bank_img_grad_abs)
# 二值化 cv2.THRESH_OTSU会选择合适的阈值进行二值化 cv2.threshold返回的是两个元素 第二个是处理后的图像
bank_img_bin = cv2.threshold(bank_img_grad_abs, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
imshow('s',bank_img_bin)
- 选择合适的参数直接二值化
bank_img_bin = cv2.threshold(bank_img_tophat,40,255,cv2.THRESH_BINARY)[1]
imshow('b',bank_img_bin)
选择合适参数二值化看起来效果很好,但是可能对于不同图像的阈值肯定不同,或许无法做到一个模型处理一堆图片。暂时将两处理封成函数,然后进行下一步。
- 多次对二值图像闭运算腐蚀数字发现效果并不好
- 要对图像闭运算腐蚀数字,可以先对灰度图进行闭运算。再进行二值化!
- 二值化前可以闭运算反复横跳
- 图像过大对腐蚀的效果影响较大,得不出较好的结果时,可以适当缩放图像
尝试了不同的组合后,我决定缩放后对灰度图像进行重复两次膨胀后闭运算、然后二值化,腐蚀数字区域
# 图像灰度化后加上缩放
bank_img_gray = num_resize(bank_img_gray,h_size=200)
# 闭操作
bank_img_close = cv2.morphologyEx(bank_img_grad_abs,cv2.MORPH_DILATE,sqKernel)
bank_img_close = cv2.morphologyEx(bank_img_close,cv2.MORPH_CLOSE,sqKernel)
imshow('bank_img_close',bank_img_close)
# 二值化 cv2.THRESH_OTSU会选择合适的阈值进行二值化 cv2.threshold返回的是两个元素 第二个是处理后的图像
bank_img_close= cv2.morphologyEx(bank_img_close,cv2.MORPH_CLOSE,sqKernel)
bank_img_close_bin = cv2.threshold(bank_img_close, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
imshow('double-close',bank_img_close_bin )
- 轮廓提取
bank_img_contour,_ = cv2.findContours(bank_img_close_bin,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(bank_img_gray,bank_img_contour,-1,(0,0,255),3)
imshow('contours',bank_img_gray)
- 获取轮廓外接矩形
模板中的数字和银行卡上宽高比差距不大, 4个数字为一组 可以计算出大概比例为2.5
num_rect_list[0][2]*4/num_rect_list[0][3]
或者通过以下代码找一组符合要求的轮廓,计算所需参数
(x,y,w,h) = cv2.boundingRect(bank_img_contour[4])
bank_img_draw = bank_img_gray
bank_img_draw = cv2.rectangle(bank_img_draw,(x,y),(x+w,y+h),(0,0,255),2)
imshow('1',bank_img_draw)
获取此时w,h,以及长宽比
print('w='+str(w)+'h='+str(h),"r="+str(w/float(h)))
过滤不合要求的图像
# 获取轮廓外接矩形 并过滤不合格的轮廓
bank_img_real_contour=[]
for contour in bank_img_contour:
(x, y, w, h) = cv2.boundingRect(contour)
r = w / float(h)
if r > 2.5 and r < 4.0:
if w > 50 and w < 80 and h > 10 and h < 30:
bank_img_real_contour.append(contour)
# 画出来看看
bank_draw = cv2.rectangle(bank_img_gray, (x, y), (x + w, y + h), (0, 0, 255), 2)
imshow('s', bank_draw)
全部正确获取
- 获取单个数字图像
# 4个一组 获取对应二值图像
bank_img_list = []
# 把4组从左往右排序 返回每组的(x,y,w,h)
contour_list = num_cnts_sort(bank_img_real_contour)
for contour in contour_list:
(x, y, w, h) = contour
# 把每组的灰度图像填充5个像素截取下来
bank_img = bank_img_gray[(y - 5):(y + 5 + h), (x - 5):(x + 5 + w)]
# 二值化
bank_img = cv2.threshold(bank_img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
imshow('bank_img', bank_img)
bank_img_list.append(bank_img)
- 对每个数字模板匹配
# 获取每个数字进行模板匹配
grade = []
for img in bank_img_list:
# 对包含4个数字的图片进行轮廓检测
bank_contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 对每个数字排序 返回的是每个轮廓外接矩形的(x,y,w,h)
bank_cont_rec = num_cnts_sort(bank_contours)
for i, rec in enumerate(bank_cont_rec):
(x, y, w, h) = rec
num = img[y:(y + h), x:(x + w)]
# 缩放到和模板一样大小
roi = cv2.resize(num, (57, 88))
item = 0
# 字典num_rect_dic存有数字和对应图像
for num in range(10):
# 模板匹配
num_img = num_rect_dic[num]
# 模板匹配
result = cv2.matchTemplate(roi, num_img, cv2.TM_CCOEFF)
(_, score, _, _) = cv2.minMaxLoc(result)
# 记下最大值,最贴近正确值得对应的 num
if score > item:
item = score
max = num
grade.append(str(max))
# cv2.putText(图像, 文字, 左下角坐标, 字体, 大小, 颜色, 字体粗细)
cv2.putText(img_draw, ''.join(grade), (contour_list[0][0], contour_list[0][1] - 15), cv2.FONT_HERSHEY_PLAIN, 1,
(0, 255, 0), 1)
imshow('bank', img_draw)
# .join把序列的字符串和前面的拼在一起
print('银行卡号为' + ''.join(grade))
这篇文章,虎头蛇尾,写到后面确实有点心累了 。下次再见。
附全部代码及图片
import cv2
import numpy as np
from imutils import contours
from matplotlib import pyplot as plt
# 定义绘图函数
def imshow(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def num_cnts_sort(list,right=1,up=0):
# 传入的是找到的轮廓,返回的是排序好的轮廓外接矩阵的(x,y,w,h)
# up=1表示从上往下,right=1表示从左往右,-1表示反过来
reverse = False
if up==-1 or right== -1:
reverse = True
if up == 0:
# 左右方向排序 权重选x
i = 0
if right == 0:
i = 1
# 找到的轮廓用外接矩形框起来 cv2.boundingRect(c)返回x,y,w,h
boundingBoxs = [cv2.boundingRect(c) for c in list]
# sorted(输入序列,排序规则,reverse=True由小到大否则由大到小)
# lambda 匿名函数 输入序列的每个元素 输出b[i]
boxs = sorted(boundingBoxs,key= lambda b: b[i],reverse=reverse )
return boxs
def num_resize(img,w_size=0,h_size=0):
(h,w)=img.shape[0:2] # size返回总元素个数 和matlab不一样
if h_size != 0:
r = h_size/float(h)
w_size = int(r*w)
if w_size != 0:
r = w_size/float(w)
h_size = int(r*h)
resized = cv2.resize(img,(w_size,h_size))
return resized
# 读取模板图片
img_num = cv2.imread('images/ocr_a_reference.png')
# cv2.cvtColor获得图像的副本
img_num_gray = cv2.cvtColor(img_num, cv2.COLOR_BGR2GRAY)
imshow('img_num',img_num)
# cv2.threshold(输入图像,阈值,赋值,方法) 这里方法是高于阈值取0,低于阈值取255
# cv2.threshold返回两个值 第二个值是我需要的处理后的图像
img_num_bin = cv2.threshold(img_num_gray,10,255,cv2.THRESH_BINARY_INV)[1]
imshow('img_num_bin',img_num_bin)
# 获取轮廓
# cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图),cv2.RETR_EXTERNAL只检测外轮廓,cv2.CHAIN_APPROX_SIMPLE只保留终点坐标
# 返回的list中每个元素都是图像中的一个轮廓
num_cnts_list, _ =cv2.findContours(img_num_bin.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
'''
cv2.drawContours(img_num, num_cnts_list, -1, (0,0,255), 2)
imshow('draw_img_num',img_num_bin)
'''
# 对轮廓排序 并且返回论廓外接矩形的坐标
num_rect_list = num_cnts_sort(num_cnts_list)
# 验证排序正确
'''
for num_rect in num_rect_list:
(x,y,w,h)=num_rect
num_rect_img = cv2.rectangle(img_num.copy(),(x,y),(x+w,y+h),(255,0,0),2)
imshow('num_rect_img',num_rect_img)
'''
# 把图片和数字对应
num_rect_dic = {}
for (i,num_rect) in enumerate(num_rect_list):
(x, y, w, h) = num_rect
# 对图片像素点操作x,y要对调,因为dim=0存的是行 是x方向的像素信息
num_rect_item = img_num_bin[y:y+h,x:x+w]
num_rect_item = cv2.resize(num_rect_item,(57,88))
# 把数字和截下来的图像对应
num_rect_dic[i]=num_rect_item
imshow('num_rect_item', num_rect_item)
# 对银行卡图像预处理
# 读取图像
bank_img = cv2.imread('images/credit_card_01.png')
bank_img = num_resize(bank_img,h_size=200)
bank_img_gray = cv2.cvtColor(bank_img,cv2.COLOR_BGR2GRAY)
# bank_img_gray = num_resize(bank_img_gray,h_size=200)
# bank_img = cv2.resize(bank_img,bank_img_gray.shape)
imshow('bank_img',bank_img)
imshow('bank_img_gray',bank_img_gray)
# 定义卷积核
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3)) # 矩形卷积核
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# 顶帽操作 突出明亮的部分
bank_img_tophat = cv2.morphologyEx(bank_img_gray,cv2.MORPH_TOPHAT, rectKernel)
imshow('bank_img_tophat',bank_img_tophat)
# 对x方向边缘检测分支 然后二值化
def branch1(bank_img_tophat):
# X方向边缘检测处理 横线太浅 y方向边缘检测可能会消失
bank_img_grad = cv2.Sobel(bank_img_tophat, cv2.CV_32F, 1, 0, ksize=-1)
bank_img_grad_abs = np.absolute(bank_img_grad)
(max, min) = (np.max(bank_img_grad_abs), np.min(bank_img_grad_abs))
bank_img_grad_abs = (255 * (bank_img_grad_abs - min) / (max - min))
bank_img_grad_abs = bank_img_grad_abs.astype('uint8')
imshow('bank_img_grad_abs', bank_img_grad_abs)
return bank_img_grad_abs
bank_img_grad_abs = branch1(bank_img_tophat)
# 腐蚀与闭操作
bank_img_close = cv2.morphologyEx(bank_img_grad_abs,cv2.MORPH_DILATE,sqKernel)
bank_img_close = cv2.morphologyEx(bank_img_close,cv2.MORPH_CLOSE,sqKernel)
imshow('bank_img_close',bank_img_close)
bank_img_close= cv2.morphologyEx(bank_img_close,cv2.MORPH_CLOSE,sqKernel)
# 二值化 cv2.THRESH_OTSU会选择合适的阈值进行二值化 cv2.threshold返回的是两个元素 第二个是处理后的图像
bank_img_close_bin = cv2.threshold(bank_img_close, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
imshow('double-close',bank_img_close_bin )
# 获取轮廓
bank_img_gray1 = bank_img_gray.copy()
bank_img_contour,_ = cv2.findContours(bank_img_close_bin,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
'''
cv2.drawContours(bank_img_gray,bank_img_contour,-1,(0,0,255),3)
imshow('contours',bank_img_gray)
'''
# 通过以下代码找到一组银行卡上的轮廓 计算大概的比例和长度
'''
(x,y,w,h) = cv2.boundingRect(bank_img_contour[4])
bank_img_draw = bank_img_gray
bank_img_draw = cv2.rectangle(bank_img_draw,(x,y),(x+w,y+h),(0,0,255),2)
imshow('1',bank_img_draw)
print('w='+str(w)+'h='+str(h),"r="+str(w/float(h)))
'''
# 获取轮廓外接矩形 并过滤不合格的轮廓
bank_img_real_contour=[]
for contour in bank_img_contour:
(x, y, w, h) = cv2.boundingRect(contour)
r = w / float(h)
if r > 2.5 and r < 4.0:
if w > 50 and w < 80 and h > 10 and h < 30:
bank_img_real_contour.append(contour)
# 画出来看看
img_draw = cv2.cvtColor(bank_img,1)
bank_draw = cv2.rectangle(img_draw, (x, y), (x + w, y + h), (0, 128, 128), 2)
imshow('s', bank_draw)
# 4个一组 获取对应二值图像
bank_img_list = []
# 把4组从左往右排序 返回每组的(x,y,w,h)
contour_list = num_cnts_sort(bank_img_real_contour)
for contour in contour_list:
(x, y, w, h) = contour
# 把每组的灰度图像填充5个像素截取下来
bank_img = bank_img_gray[(y - 5):(y + 5 + h), (x - 5):(x + 5 + w)]
# 二值化
bank_img = cv2.threshold(bank_img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
imshow('bank_img', bank_img)
bank_img_list.append(bank_img)
# 获取每个数字进行模板匹配
grade = []
for img in bank_img_list:
# 对包含4个数字的图片进行轮廓检测
bank_contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 对每个数字排序 返回的是每个轮廓外接矩形的(x,y,w,h)
bank_cont_rec = num_cnts_sort(bank_contours)
for i, rec in enumerate(bank_cont_rec):
(x, y, w, h) = rec
num = img[y:(y + h), x:(x + w)]
# 缩放到和模板一样大小
roi = cv2.resize(num, (57, 88))
item = 0
# 字典num_rect_dic存有数字和对应图像
for num in range(10):
# 模板匹配
num_img = num_rect_dic[num]
# 模板匹配
result = cv2.matchTemplate(roi, num_img, cv2.TM_CCOEFF)
(_, score, _, _) = cv2.minMaxLoc(result)
# 记下最大值,最贴近正确值得对应的 num
if score > item:
item = score
max = num
grade.append(str(max))
# cv2.putText(图像, 文字, 左下角坐标, 字体, 大小, 颜色, 字体粗细)
cv2.putText(img_draw, ''.join(grade), (contour_list[0][0], contour_list[0][1] - 15), cv2.FONT_HERSHEY_PLAIN, 1,
(0, 255, 0), 1)
imshow('bank', img_draw)
# .join把序列的字符串和前面的拼在一起
print('银行卡号为' + ''.join(grade))
- 银行卡及模板图片