基于OpenCV的银行卡识别

一、设计思路

1、预处理银行卡号序列模版,对其进行一系列形态学操作,继而进行轮廓识别,构建与各个轮廓所对应的数字元组。

2、对将要识别的银行卡进行灰度处理、二值化、阈值处理,sobel算子边缘检测等预处理,再通过模版匹配方法找出与已知轮廓高度符合的数字。

二、代码复现

预操作:

自定义一个cv_show函数,便于后来的图像展示。

# 绘图展示
def cv_show(name,img):
	cv2.imshow(name, img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
#对于myutils模块,在其中定义一个对模版排序的函数
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
#在自定义模块myutils中定义一个resize函数,可以自定义图像大小
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 N

你可能感兴趣的:(OpenCV,opencv,人工智能,计算机视觉)