本篇內(nèi)容主要講解“Python基于模板怎么實(shí)現(xiàn)匹配信用卡數(shù)字識(shí)別功能”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Python基于模板怎么實(shí)現(xiàn)匹配信用卡數(shù)字識(shí)別功能”吧!
創(chuàng)新互聯(lián)建站長(zhǎng)期為上千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為城固企業(yè)提供專業(yè)的做網(wǎng)站、成都網(wǎng)站制作,城固網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
Python 3.6 + OpenCV 3.4.1.15
首先,提取出模板中每一個(gè)數(shù)字的輪廓,再對(duì)信用卡圖像進(jìn)行處理,提取其中的數(shù)字部分,將該部分?jǐn)?shù)字與模板進(jìn)行匹配,即可得到結(jié)果。
完整代碼
# !/usr/bin/env python # —*— coding: utf-8 —*— # @Time: 2020/1/11 14:57 # @Author: Martin # @File: utils.py # @Software:PyCharm 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] (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): (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 # !/usr/bin/env python # —*— coding: utf-8 —*— # @Time: 2020/1/11 14:57 # @Author: Martin # @File: template_match.py # @Software:PyCharm """ 基于模板匹配的信用卡數(shù)字識(shí)別 """ import cv2 import utils import numpy as np # 指定信用卡類型 FIRST_NUMBER = { '3' : 'American Express', '4' : 'Visa', '5' : 'MasterCard', '6' : 'Discover Card' } # 繪圖顯示 def cv_show(name, image): cv2.imshow(name, image) cv2.waitKey(0) cv2.destroyAllWindows() # 讀取模板圖像 img = cv2.imread('./images/ocr_a_reference.png') cv_show('img', img) # 轉(zhuǎn)化成灰度圖 ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv_show('ref', ref) # 轉(zhuǎn)化成二值圖像 ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1] cv_show('ref', ref) # 計(jì)算輪廓 ref_, 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 = utils.sort_contours(refCnts, method='left-to-right')[0] digits = {} # 遍歷每一個(gè)輪廓 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 # 初始化卷積核 rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3)) sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) # 讀取輸入圖像,預(yù)處理 img_path = input("Input the path and image name: ") image_input = cv2.imread(img_path) cv_show('image', image_input) image_input = utils.resize(image_input, width=300) gray = cv2.cvtColor(image_input, cv2.COLOR_BGR2GRAY) cv_show('gray', gray) # 禮帽操作,突出更明亮的區(qū)域 tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel) cv_show('tophat', tophat) gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1) gradX = np.absolute(gradX) (minVal, maxVal) = (np.min(gradX), np.max(gradX)) gradX = (255 * ((gradX - minVal) / (maxVal - minVal))) gradX = gradX.astype("uint8") print(np.array(gradX).shape) cv_show('gradX', gradX) # 閉操作 gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel) cv_show('gradX', gradX) thresh = cv2.threshold(gradX, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] cv_show('thresh', thresh) thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel) cv_show('thresh', thresh) # 計(jì)算輪廓 thresh_, threshCnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = threshCnts cur_img = image_input.copy() cv2.drawContours(cur_img, cnts, -1, (0, 0, 255), 3) cv_show('img', cur_img) locs = [] # 遍歷輪廓 for (i, c) in enumerate(cnts): (x, y, w, h) = cv2.boundingRect(c) ar = w / float(h) if 2.5 < ar < 4.0 and (40 < w < 55) and (10 < h < 20): locs.append((x, y, w, h)) # 將符合的輪廓從左到右排序 locs = sorted(locs, key=lambda ix: ix[0]) output = [] # 遍歷每一個(gè)輪廓中的數(shù)字 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) # 預(yù)處理 group = cv2.threshold(group, 0, 255, cv2.THRESH_OTSU)[1] cv_show('group', group) # 計(jì)算每一組輪廓 group_, digitCnts, hierarchy = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) digitCnts = utils.sort_contours(digitCnts, method='left-to-right')[0] # 計(jì)算每一組的每個(gè)數(shù)值 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) (_, score, _, _) = cv2.minMaxLoc(result) scores.append(score) # 得到最合適的數(shù)字 groupOutput.append(str(np.argmax(scores))) cv2.rectangle(image_input, (gX - 5, gY - 5), (gX + gW + 5, gY + gH + 5), (0, 0, 255), 1) cv2.putText(image_input, "".join(groupOutput), (gX, gY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2) # 得到結(jié)果 output.extend(groupOutput) # 打印結(jié)果 print("Credit Card Type: {}".format(FIRST_NUMBER[output[0]])) print("Credit Card #: {}".format("".join(output))) cv2.imshow("Image", image_input) cv2.waitKey(0) cv2.destroyAllWindows()
結(jié)果展示
Credit Card Type: Visa Credit Card #: 4020340002345678
到此,相信大家對(duì)“Python基于模板怎么實(shí)現(xiàn)匹配信用卡數(shù)字識(shí)別功能”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文名稱:Python基于模板怎么實(shí)現(xiàn)匹配信用卡數(shù)字識(shí)別功能
地址分享:http://www.muchs.cn/article44/jsoche.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站排名、自適應(yīng)網(wǎng)站、網(wǎng)站維護(hù)、全網(wǎng)營(yíng)銷推廣、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)