python信用卡识别_python opencv实现信用卡的数字识别

该博客介绍了一个使用Python及OpenCV库来识别信用卡数字的项目。首先,介绍了所需工具包的导入,然后详细阐述了图像预处理步骤,包括灰度化、二值化、边缘检测和轮廓提取等。接着,展示了如何通过模板匹配进行数字检测,并提供了对不同信用卡类型的处理。最后,博主分享了用于调整图像尺寸和显示图像的辅助函数。
摘要由CSDN通过智能技术生成

本项目利用python以及opencv实现信用卡的数字识别

前期准备

导入工具包

定义功能函数

模板图像处理

读取模板图像 cv2.imread(img)

灰度化处理 cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

二值化 cv2.threshold()

轮廓 - 轮廓

信用卡图像处理

读取信用卡图像 cv2.imread(img)

灰度化处理 cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

礼帽处理 cv2.morphologyEx(gray,cv2.MORPH_TOPHAT,rectKernel)

Sobel边缘检测 cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)

闭操作 cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)

计算轮廓 cv2.findContours

模板检测 cv2.matchTemplate(roi, digitROI,cv2.TM_CCOEFF)

原始数据展示

362d5f18816346773e3782b252687a8c.png

6140287251b9823b4833d7be550d26cf.png

fbca71282cde62388237d5d1a134db37.png

00e771fed3326f5f0ce075aa87e1619b.png

f3761bfb2ca15536685978ebde2b1cad.png

975cef802019dfb32fc7ff347e249d8c.png

结果展示

772255cf2f7e6ea4c53eb26d78832007.png

1 前期准备

# 导入工具包

# opencv读取图片的格式为b g r

# matplotlib图片的格式为 r g b

import numpy as np

import cv2

from imutils import contours

import matplotlib.pyplot as plt

%matplotlib inline

# 信用卡的位置

predict_card = "images/credit_card_01.png"

# 模板的位置

template = "images/ocr_a_reference.png"

# 指定信用卡类型

FIRST_NUMBER = {

"3": "American Express",

"4": "Visa",

"5": "MasterCard",

"6": "Discover Card"

}

# 定义一些功能函数

# 对框进行排序

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

# 定义cv2展示函数

def cv_show(name,img):

cv2.imshow(name,img)

cv2.waitKey(0)

cv2.destroyAllWindows()

2 对模板图像进行预处理操作

读取模板图像

# 读取模板图像

img = cv2.imread(template)

cv_show("img",img)

plt.imshow(img)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值