银行卡卡号识别步骤
1.对模板数字进行处理,得到每个模板的数字。
代码如下(示例):
import cv2
import numpy as np
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(cnt)for cnt in cnts]
(cnts,boundingBoxes) = zip(*sorted(zip(cnts,boundingBoxes),
key = lambda b: b[1][i],
reverse = reverse))
return cnts,boundingBoxes
def cv_show(name,img):#定义一个函数
cv2.imshow(name,img)
cv2.waitKey()
cv2.destroyAllWindows()
img = cv2.imread('ocr_a_reference.png')#读模板
# cv_show('img',img)
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#色彩空间转化,转化为灰度图
# cv_show('img_gray',img_gray)
ret,img_binary = cv2.threshold(img_gray,127,255,1)#二值化
cv_show('img_binary',img_binary)
cnts,hierarchy = cv2.findContours(img_binary,cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
#查找外轮廓
img_rect = cv2.drawContours(img,cnts,-1,(0,0,255),2)# 绘制轮廓