亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

環(huán)境介紹

Python 3.6 + OpenCV 3.4.1.15

原理介紹

首先,提取出模板中每一個數字的輪廓,再對信用卡圖像進行處理,提取其中的數字部分,將該部分數字與模板進行匹配,即可得到結果。

模板展示

在這里插入圖片描述

完整代碼

# !/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
"""
基于模板匹配的信用卡數字識別
"""
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)
# 轉化成灰度圖
ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv_show('ref', ref)
# 轉化成二值圖像
ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]
cv_show('ref', ref)
# 計算輪廓
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 = {}
# 遍歷每一個輪廓
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))
# 讀取輸入圖像,預處理
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)
# 計算輪廓
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 = []
# 遍歷每一個輪廓中的數字
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)
 # 預處理
 group = cv2.threshold(group, 0, 255, cv2.THRESH_OTSU)[1]
 cv_show('group', group)
 # 計算每一組輪廓
 group_, digitCnts, hierarchy = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 digitCnts = utils.sort_contours(digitCnts, method='left-to-right')[0]
 # 計算每一組的每個數值
 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)
 # 得到最合適的數字
 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)
 # 得到結果
 output.extend(groupOutput)
# 打印結果
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()

結果展示

在這里插入圖片描述

Credit Card Type: Visa
Credit Card #: 4020340002345678

總結

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

相關文章

  • Python實現獲取當前日期的所屬信息

    Python實現獲取當前日期的所屬信息

    在Python中,處理日期和時間是一個常見的任務,它涉及到許多方面,例如獲取日期的年、月、日、星期幾等等,本文將詳細介紹如何使用Python來獲取當前日期的各種相關信息,需要的可以了解下
    2024-01-01
  • python tkinter控件treeview的數據列表顯示的實現示例

    python tkinter控件treeview的數據列表顯示的實現示例

    本文主要介紹了python tkinter控件treeview的數據列表顯示的實現示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • python3.7實現云之訊、聚合短信平臺的短信發(fā)送功能

    python3.7實現云之訊、聚合短信平臺的短信發(fā)送功能

    這篇文章主要介紹了python3.7實現云之訊、聚合短信平臺的短信發(fā)送功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • python Django框架實現自定義表單提交

    python Django框架實現自定義表單提交

    這篇文章主要為大家詳細介紹了Django框架實現自定義表單提交,針對"表單提交"和"Ajax提交"兩種方式來解決CSRF帶來的錯誤進行講解,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 5個Python使用F-String進行格式化的實用技巧分享

    5個Python使用F-String進行格式化的實用技巧分享

    F-String(格式化字符串字面值)是在Python?3.6中引入的,它是一種非常強大且靈活的字符串格式化方法,本文總結了5個實用的F-String技巧,相信一定能讓你的代碼輸出更加的美觀,快跟隨小編一起學習起來吧
    2024-03-03
  • Python實現創(chuàng)建模塊的方法詳解

    Python實現創(chuàng)建模塊的方法詳解

    導入一個模塊,我們一般都會使用?import?關鍵字,但有些場景下?import?難以滿足我們的需要。所以除了?import?之外還有很多其它導入模塊的方式,下面就來介紹一下
    2022-07-07
  • python3操作注冊表的方法(Url protocol)

    python3操作注冊表的方法(Url protocol)

    使用python操作注冊表的方法最近學習了一下,現在做一下筆記,由于對Python語言的使用還不是很熟練,所以寫不出高大上的結構
    2020-02-02
  • 憶童年!用Python實現憤怒的小鳥游戲

    憶童年!用Python實現憤怒的小鳥游戲

    好久都沒玩過憤怒的小鳥了,今天咱自己做一個玩玩,文中有非常詳細的代碼示例,對想玩的小伙伴們很有用哦,需要的朋友可以參考下
    2021-06-06
  • mac 上配置Pycharm連接遠程服務器并實現使用遠程服務器Python解釋器的方法

    mac 上配置Pycharm連接遠程服務器并實現使用遠程服務器Python解釋器的方法

    這篇文章主要介紹了mac 上如何配置Pycharm連接遠程服務器并實現使用遠程服務器Python解釋器,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • pygame實現貪吃蛇游戲

    pygame實現貪吃蛇游戲

    這篇文章主要為大家詳細介紹了pygame實現貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01

最新評論