最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python開發(fā)之基于模板匹配的信用卡數(shù)字識別功能

 更新時(shí)間:2020年01月13日 10:09:30   作者:虐貓人薛定諤i  
這篇文章主要介紹了基于模板匹配的信用卡數(shù)字識別功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

環(huán)境介紹

Python 3.6 + OpenCV 3.4.1.15

原理介紹

首先,提取出模板中每一個(gè)數(shù)字的輪廓,再對信用卡圖像進(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ù)字識別
"""
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

總結(jié)

以上所述是小編給大家介紹的Python開發(fā)之基于模板匹配的信用卡數(shù)字識別功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • python中如何使用虛擬環(huán)境

    python中如何使用虛擬環(huán)境

    這篇文章主要介紹了python中如何使用虛擬環(huán)境,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-10-10
  • pycharm的python_stubs問題

    pycharm的python_stubs問題

    這篇文章主要介紹了pycharm的python_stubs問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python flask解析json數(shù)據(jù)不完整的解決方法

    python flask解析json數(shù)據(jù)不完整的解決方法

    這篇文章主要介紹了python flask解析json數(shù)據(jù)不完整的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05
  • python區(qū)塊鏈簡易版交易實(shí)現(xiàn)示例

    python區(qū)塊鏈簡易版交易實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了python區(qū)塊鏈簡易版交易實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Python基類函數(shù)的重載與調(diào)用實(shí)例分析

    Python基類函數(shù)的重載與調(diào)用實(shí)例分析

    這篇文章主要介紹了Python基類函數(shù)的重載與調(diào)用方法,實(shí)例分析了Python中基類函數(shù)的重載及調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • Python爬蟲輔助利器PyQuery模塊的安裝使用攻略

    Python爬蟲輔助利器PyQuery模塊的安裝使用攻略

    這篇文章主要介紹了Python爬蟲輔助利器PyQuery模塊的安裝使用攻略,PyQuery可以方便地用來解析HTML內(nèi)容,使其成為眾多爬蟲程序開發(fā)者的大愛,需要的朋友可以參考下
    2016-04-04
  • pandas 快速處理 date_time 日期格式方法

    pandas 快速處理 date_time 日期格式方法

    今天小編就為大家分享一篇pandas 快速處理 date_time 日期格式方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python中如何使用insert函數(shù)

    python中如何使用insert函數(shù)

    這篇文章主要介紹了python中如何使用insert函數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Python實(shí)現(xiàn)在線程里運(yùn)行scrapy的方法

    Python實(shí)現(xiàn)在線程里運(yùn)行scrapy的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)在線程里運(yùn)行scrapy的方法,涉及Python線程操作的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Python+NumPy繪制常見曲線的方法詳解

    Python+NumPy繪制常見曲線的方法詳解

    NumPy(Numerical Python)是Python的一種開源的數(shù)值計(jì)算擴(kuò)展。本文將利用NumPy庫繪制利薩茹曲線、計(jì)算斐波那契數(shù)列、方波和鋸齒波和三角波,需要的可以參考一下
    2022-06-06

最新評論

新龙县| 岢岚县| 固始县| 灌阳县| 南丹县| 博白县| 灌阳县| 虞城县| 东乡县| 临邑县| 土默特右旗| 迭部县| 桃源县| 安溪县| 行唐县| 广东省| 石河子市| 泗洪县| 牟定县| 宜宾市| 澎湖县| 邯郸市| 日喀则市| 吉隆县| 伊川县| 左云县| 邳州市| 吉林省| 朝阳县| 常山县| 包头市| 嘉善县| 临江市| 灌云县| 郓城县| 阜平县| 德昌县| 安庆市| 赤城县| 东安县| 江孜县|