项目概述:

基于opencv实现信用卡数字识别,如下图所示:

android opencv 数字识别 基于opencv的数字识别_预处理

项目流程如下:

1.处理模板,进行轮廓检测(检测外轮廓)

2.得到当前轮廓的外接矩形,并将模板中的外接矩形切割出来,得到0-9对应的模板图片,并resize

3.使用形态学操作对信用卡图片进行处理,得到轮廓

4.根据矩形轮廓的长宽比挑选出信用卡的数字矩形框,并resize

5.使用for循环依次检测

代码如下:

ocr_template_match.py

1 # 导入工具包
  2 from imutils import contours
  3 import numpy as np
  4 import argparse
  5 import cv2
  6 import myutils
  7  
  8 # 设置参数
  9 ap = argparse.ArgumentParser()
 10 ap.add_argument("-i", "--image", default='images/credit_card_01.png',
 11     help="path to input image")
 12 ap.add_argument("-t", "--template", default='ocr_a_reference.png',
 13     help="path to template OCR-A image")
 14 args = vars(ap.parse_args())
 15  
 16 # 指定信用卡类型
 17 FIRST_NUMBER = {
 18     "3": "American Express",
 19     "4": "Visa",
 20     "5": "MasterCard",
 21     "6": "Discover Card"
 22 }
 23 # 绘图展示
 24 def cv_show(name,img):
 25     cv2.imshow(name,img)
 26     cv2.waitKey(0)
 27     cv2.destroyAllWindows()
 28 # 读取一个模板图像
 29 img = cv2.imread(args["template"])
 30 cv_show('img',img)
 31 # 灰度图
 32 ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 33 cv_show('ref',ref)
 34 # 二值图像
 35 ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]
 36 cv_show('ref',ref)
 37  
 38 # 计算轮廓
 39 #cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图),cv2.RETR_EXTERNAL只检测外轮廓,cv2.CHAIN_APPROX_SIMPLE只保留终点坐标
 40 #返回的list中每个元素都是图像中的一个轮廓
 41  
 42 ref_, refCnts, hierarchy = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
 43  
 44 cv2.drawContours(img,refCnts,-1,(0,0,255),3) 
 45 cv_show('img',img)
 46 print (np.array(refCnts).shape)
 47 refCnts = myutils.sort_contours(refCnts, method="left-to-right")[0] #排序,从左到右,从上到下
 48 digits = {}
 49  
 50 # 遍历每一个轮廓
 51 for (i, c) in enumerate(refCnts):
 52     # 计算外接矩形并且resize成合适大小
 53     (x, y, w, h) = cv2.boundingRect(c)
 54     roi = ref[y:y + h, x:x + w]
 55     roi = cv2.resize(roi, (57, 88))
 56  
 57     # 每一个数字对应每一个模板
 58     digits[i] = roi
 59  
 60 # 初始化卷积核
 61 rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3))
 62 sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
 63  
 64 #读取输入图像,预处理
 65 image = cv2.imread(args["image"])
 66 cv_show('image',image)
 67 image = myutils.resize(image, width=300)
 68 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 69 cv_show('gray',gray)
 70  
 71 #礼帽操作,突出更明亮的区域
 72 tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel) 
 73 cv_show('tophat',tophat) 
 74 # 
 75 gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0, #ksize=-1相当于用3*3的
 76     ksize=-1)
 77  
 78  
 79 gradX = np.absolute(gradX)
 80 (minVal, maxVal) = (np.min(gradX), np.max(gradX))
 81 gradX = (255 * ((gradX - minVal) / (maxVal - minVal)))
 82 gradX = gradX.astype("uint8")
 83  
 84 print (np.array(gradX).shape)
 85 cv_show('gradX',gradX)
 86  
 87 #通过闭操作(先膨胀,再腐蚀)将数字连在一起
 88 gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel) 
 89 cv_show('gradX',gradX)
 90 #THRESH_OTSU会自动寻找合适的阈值,适合双峰,需把阈值参数设置为0
 91 thresh = cv2.threshold(gradX, 0, 255,
 92     cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] 
 93 cv_show('thresh',thresh)
 94  
 95 #再来一个闭操作
 96  
 97 thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel) #再来一个闭操作
 98 cv_show('thresh',thresh)
 99  
100 # 计算轮廓
101  
102 thresh_, threshCnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
103     cv2.CHAIN_APPROX_SIMPLE)
104  
105 cnts = threshCnts
106 cur_img = image.copy()
107 cv2.drawContours(cur_img,cnts,-1,(0,0,255),3) 
108 cv_show('img',cur_img)
109 locs = []
110  
111 # 遍历轮廓
112 for (i, c) in enumerate(cnts):
113     # 计算矩形
114     (x, y, w, h) = cv2.boundingRect(c)
115     ar = w / float(h)
116  
117     # 选择合适的区域,根据实际任务来,这里的基本都是四个数字一组
118     if ar > 2.5 and ar < 4.0:
119  
120         if (w > 40 and w < 55) and (h > 10 and h < 20):
121             #符合的留下来
122             locs.append((x, y, w, h))
123  
124 # 将符合的轮廓从左到右排序
125 locs = sorted(locs, key=lambda x:x[0])
126 output = []
127  
128 # 遍历每一个轮廓中的数字
129 for (i, (gX, gY, gW, gH)) in enumerate(locs):
130     # initialize the list of group digits
131     groupOutput = []
132  
133     # 根据坐标提取每一个组
134     group = gray[gY - 5:gY + gH + 5, gX - 5:gX + gW + 5]
135     cv_show('group',group)
136     # 预处理
137     group = cv2.threshold(group, 0, 255,
138         cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
139     cv_show('group',group)
140     # 计算每一组的轮廓
141     group_,digitCnts,hierarchy = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL,
142         cv2.CHAIN_APPROX_SIMPLE)
143     digitCnts = contours.sort_contours(digitCnts,
144         method="left-to-right")[0]
145  
146     # 计算每一组中的每一个数值
147     for c in digitCnts:
148         # 找到当前数值的轮廓,resize成合适的的大小
149         (x, y, w, h) = cv2.boundingRect(c)
150         roi = group[y:y + h, x:x + w]
151         roi = cv2.resize(roi, (57, 88))
152         cv_show('roi',roi)
153  
154         # 计算匹配得分
155         scores = []
156  
157         # 在模板中计算每一个得分
158         for (digit, digitROI) in digits.items():
159             # 模板匹配
160             result = cv2.matchTemplate(roi, digitROI,
161                 cv2.TM_CCOEFF)
162             (_, score, _, _) = cv2.minMaxLoc(result)
163             scores.append(score)
164  
165         # 得到最合适的数字
166         groupOutput.append(str(np.argmax(scores)))
167  
168     # 画出来
169     cv2.rectangle(image, (gX - 5, gY - 5),
170         (gX + gW + 5, gY + gH + 5), (0, 0, 255), 1)
171     cv2.putText(image, "".join(groupOutput), (gX, gY - 15),
172         cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
173  
174     # 得到结果
175     output.extend(groupOutput)
176  
177 # 打印结果
178 print("Credit Card Type: {}".format(FIRST_NUMBER[output[0]]))
179 print("Credit Card #: {}".format("".join(output)))
180 cv2.imshow("Image", image)
181 cv2.waitKey(0)

myutils.py

import cv2
 
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(c) for c in cnts] #用一个最小的矩形,把找到的形状包起来x,y,h,w
    (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
                                        key=lambda b: b[1][i], reverse=reverse))
 
    return cnts, boundingBoxes
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)
    return resized

ocr_a_reference.png

android opencv 数字识别 基于opencv的数字识别_android opencv 数字识别_02

 

 credit_card_01.png

android opencv 数字识别 基于opencv的数字识别_git_03

 

 credit_card_02.png

android opencv 数字识别 基于opencv的数字识别_android opencv 数字识别_04

 

识别结果:

android opencv 数字识别 基于opencv的数字识别_预处理_05

android opencv 数字识别 基于opencv的数字识别_android opencv 数字识别_06