计算机视觉项目-银行卡卡号自动识别

😊😊😊 欢迎来到本博客😊😊😊
本次博客内容将继续讲解关于OpenCV的相关知识,利用项目讲解继续巩固自己得基础知识。
🎉 作者简介:⭐️⭐️⭐️ 目前计算机研究生在读。主要研究方向是人工智能和群智能算法方向。目前熟悉python网页爬虫、机器学习、计算机视觉(OpenCV)、群智能算法。然后正在学习深度学习的相关内容。以后可能会涉及到网络安全相关领域,毕竟这是每一个学习计算机的梦想嘛!计算机视觉更新完成之后我会更新相关于深度学习得相关知识。
📝 目前更新:🌟🌟🌟目前已经更新了关于网络爬虫的相关知识、机器学习的相关知识、算法小案例、目前正在更新计算机视觉-OpenCV的相关内容。

计算机视觉项目-银行卡卡号自动识别

文章目录

*
🐨前言
🐨项目说明
🐨步骤讲解
🐨全程操作讲解

; 🐨前言

在以上几个问题中,我们已经介绍了计算机视觉的所有基础处理,然后我们将继续用这里的几个小项目来巩固之前的基础知识。其实,我们博客做的银行卡号识别和车牌识别、快递单号识别等项目都是环环相扣的,所以我们掌握了这门课的相关知识,相当于掌握了很多项目的思路!让我们从今天的项目演示开始。

[En]

In the above several issues, we have introduced all the basic processing of computer vision, and then we will continue to consolidate the previous basic knowledge with a few small projects here. In fact, the bank card number recognition and license plate recognition, express order number recognition and other projects done by our blog are all interlinked, so we have mastered the relevant knowledge of this class, which is equivalent to mastering the ideas of many projects! Let’s start with today’s project presentation.

计算机视觉项目-银行卡卡号自动识别

🐨项目说明

项目目标:我们导入一张银行卡或者信用卡得图像,通过OpenCV来把它实现出来,把信用卡得数字(卡号)给识别出来。
首先,我们需要一个数字模板,比如下面两个,在项目不同的地方,我们需要一个不同的模板,那么这个模板有什么用呢?也就是说,我们必须使用OpenCV来识别银行卡上的号码,但我如何让计算机知道这些号码?我一定要让他学吗?计算机通过学习后,他知道哪个是1,哪个是2,通常的模板都有车牌模板。

[En]

First of all, we need a digital template, such as the following two, where the project is different, we need a different template, so what is the use of this template? That is to say, we have to use OpenCV to identify the numbers on the bank card, but how do I let the computer know these numbers? do I have to let him learn? after the computer passes the study, he knows which is 1 and which is 2. The usual templates have license plate templates.

计算机视觉项目-银行卡卡号自动识别

计算机视觉项目-银行卡卡号自动识别

计算机视觉项目-银行卡卡号自动识别
我们用我们学过的模型逐个匹配信用卡上的数字,然后我们要做这样的事情,然后我们让计算机为每个匹配结果打分,例如,我们发送一个图像,他是8,他首先与1匹配,给32分,2给18分,8给88分。在最高时,我们的计算机会假设数字是8。然后在相应的模板上找到索引(正如这里所解释的,这个模板的数字的型号和外观应该与目标一致。)在这里,我们需要使用RESIZE操作来调整提取的东西的大小。
[En]

We use the model we have learned to match the numbers on the credit card one by one, and we are going to do something like this, and then we ask the computer to give a score for each matching result, for example, we send in an image, he is 8, and he first matches with 1, giving 32 points, 2 giving 18 points, and 8 giving 88 points. At the highest, then our computer will assume that the number is 8. Then find the index on the corresponding template (as explained here, the model and appearance of the number of this template should be consistent with the target. ) here we need to use the resize operation to resize the extracted things.

; 🐨步骤讲解

1.使用模板匹配方式对模板,以及输入图像进行轮廓检测(检测外轮廓)。
2.得到当前轮廓的外接矩形。
3.将模板中的外接矩形切割出来。
4.使用矩形的长宽比之间的差异使得信用卡的数字矩形框能够被选择出来。
5.将其进一步细分,与需要识别的信用卡当中的外接矩形resize成同样的大小。
6.使用for循环依次检测。

计算机视觉项目-银行卡卡号自动识别

🐨全程操作讲解

我们再说明之前,先说一下学习代码不管学习什么语言吧,一定要会的一个操作就是DEBUG这个操作,会帮助你阅读代码,省时省力。比自己一行一行的去阅读,而且你也记不住哪块得到的结果是什么,DEBUG一个操作帮助你全部搞定。对于DEBUG,我们使用pycharm就是打上断点,一般就是在主程序 if __name__=='__main__':这里下方打上断点,然后按照他的指引一步一步的去看代码。 非常实用!!!
然后我们直接进入代码。首先,让我们来看看我们想要操纵的图片。

[En]

Then we go directly to the code. First, let’s take a look at the picture we want to manipulate.

计算机视觉项目-银行卡卡号自动识别
首先,我们需要定义一个绘图函数,方便后面的绘图操作。否则,每次做同样的手术都很麻烦。
[En]

First of all, we need to define a drawing function, which is convenient for later drawing operation. Otherwise, it will be troublesome to do the same operation every time.

def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

然后导入第三方库,输入参数操作:

[En]

Then we import third-party libraries and input parameter operations:

其中 imutils库,它整合了 opencv、numpy和matplotlib的相关操作,主要是用来进行图形图像的处理,如图像的 平移、旋转、缩放、骨架提取、显示等等,后期又加入了针对视频的处理,如摄像头、本地文件等。 imutils同时支持 python2和python3。
银行卡的类型在这里定义。

[En]

The type of bank card is defined here.

from imutils import contours
import numpy as np
import argparse
import cv2
import myutils

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to input image")
ap.add_argument("-t", "--template", required=True,
    help="path to template OCR-A image")
args = vars(ap.parse_args())
FIRST_NUMBER = {
    "3": "American Express",
    "4": "Visa",
    "5": "MasterCard",
    "6": "Discover Card"
}

这里我们解释一下,对于pycharm中的操作,一定要指定输入图像的路径。

[En]

Here we explain that for operations in pycharm, be sure to specify the path to the input image.

计算机视觉项目-银行卡卡号自动识别
这里我们点击Modify Run Configuration然后指定参数。
计算机视觉项目-银行卡卡号自动识别
在这里制定一个规范,输入-i,然后指定输入图像路径必须准确到图像,比如123.jpg。然后是空白-T模板的图片。如果你在这里有什么不明白的,你可以私下问我。
[En]

Make a specification here, type-I and then specify that the input image path must be accurate to the image, such as 123.jpg. Then the picture of the blank-t template. If there is anything you don’t understand here, you can ask me privately.

-i C:\Users\jzdx\Desktop\OpenCV\xinyongka\template-matching-ocr\images\credit_card_01.png
-t C:\Users\jzdx\Desktop\OpenCV\xinyongka\template-matching-ocr\images\ocr_a_reference.png

这里有500多个色彩空间转换。

[En]

There are more than 500 color space conversions here.

img = cv2.imread(args["template"])
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)

在这里,我们转换模板的灰度,然后进行二进制运算,对应的二进制结果是:

[En]

Here, we convert the grayscale of the template, and then the binary operation, and the corresponding binary result is:

计算机视觉项目-银行卡卡号自动识别
对模板进行轮廓的提取。 cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图), cv2.RETR_EXTERNAL只检测外轮廓, cv2.CHAIN_APPROX_SIMPLE只保留终点坐标。这里需要注意的就是,Opencv老版本返回的是三个参数,而新的版本轮廓检测只返回两个参数。然后画出指定的外轮廓。
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)

计算机视觉项目-银行卡卡号自动识别
提取的模板的轮廓。
[En]

The outline of the extracted template.

refCnts = myutils.sort_contours(refCnts, method="left-to-right")[0]

我们对大纲进行排序,那么如何对其进行排序,使用大纲的横坐标对其进行排序,这里我们直接跳到myutils程序。

[En]

We sort the outline, so how to sort it, sort it using an Abscissa of the outline, here we jump directly into the myutils program.

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

这里主要是做一个排序操作的大纲,把1放在1的位置上,没有排序是混乱的。这里对于排序这个操作是比较常用的操作,朋友们应该掌握了。

[En]

Here is mainly to do an outline of the sorting operation, put 1 on the position of 1, there is no sorting is chaotic. Here for sorting this operation is a more commonly used operation, you friends should master.

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))

这里对模板遍历了每一个轮廓,进行了resize一下。并且这里定义了两个卷积核。后期后用到。 cv2.getStructuringElement设置卷积核的意思。

image = cv2.imread(args["image"])
cv_show('image',image)
image = myutils.resize(image, width=300)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv_show('gray',gray)
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)

这里,对传入的图像进行了预处理操作,包括灰度转换、突出较亮区域的礼貌操作,然后是Sobel操作。然后,Sobel意味着用特定的卷积核对图像进行卷积。

[En]

Here, a preprocessing operation is performed on the incoming image, including * conversion to grayscale, politeness operation to highlight brighter areas * , and then a * Sobel operation. Then Sobel means to convolution an image with a specific convolution kernel.

计算机视觉项目-银行卡卡号自动识别
计算机视觉项目-银行卡卡号自动识别
计算机视觉项目-银行卡卡号自动识别
再来一次封闭行动。膨胀,然后腐蚀!那么目的是什么?我们希望将这四件物品的所有银行卡号码连接在一起,然后找到它并提取出来。这就是我想要的。
[En]

One more closed operation. *

expand and then corrode! * * so what is the purpose? we want all the bank card numbers of these four pieces to be connected together, then find it and extract it. That’s what I want.
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)

这里的二元运算是自动找到一个合适的值作为阈值,而不是0。0。

[En]

The binary operation here is to automatically find an appropriate value as the threshold, rather than 0. 0.

thresh = cv2.threshold(gradX, 0, 255,
    cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

这个代码部分的重点是,他明显是在做一个阈值运算,也就是说我们要把图像处理成一个二值图像,那么我们怎么处理呢?在图中,不是将阈值设置为0,而是让计算机自动识别最优阈值!

[En]

The key point of this code part is that he is obviously doing a threshold operation, that is to say, we have to process the image into a binary image, so how do we deal with it? In the picture, instead of setting the threshold to 0, let the computer automatically identify the optimal threshold!

计算机视觉项目-银行卡卡号自动识别
第二,研究结果如下:
[En]

Second, the results are as follows:

计算机视觉项目-银行卡卡号自动识别
在这里,一位小朋友问道:你在做什么?我们所做的事情的目的是摧毁我们想要的区域。目前我们发现第一个区块和第四个区块已经连接,中间的第二个区块和第三个区块还没有连接,所以我们再做一次关闭操作。
[En]

Here, a little friend asked, what are you doing? the purpose of what we are doing is to take out the area we want. At present, we find that the first block and the fourth block have been connected, and the middle second block and the third block have not been connected, so let’s do the closing operation again.

thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)
cv_show('thresh',thresh)

正在执行关闭操作。因为我们发现有些地方还有一个黑色的空缺,我们要全部填上255,也就是白色。

[En]

A closed operation is being performed. Because we found that there is still a black vacancy in some places, we have to fill it all with 255, that is, white.

threshCnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)

cnts = threshCnts
cur_img = image.copy()
cv2.drawContours(cur_img,cnts,-1,(0,0,255),3)
cv_show('img',cur_img)

经过预处理后,我们对原始图像进行了轮廓采集。他使用我们处理过的二值图像来获取轮廓信息。

[En]

After the preprocessing, we make an outline acquisition of the original image. He uses the binary image we processed to obtain the contour information.

计算机视觉项目-银行卡卡号自动识别
有这么多轮廓,我想要哪一个?在这里,我们需要执行过滤操作。这取决于你自己的项目。我可能会试几次。
[En]

With so many contours, which one do I want? here we need to carry out a filtering operation. It depends on your own project. I’ll probably try it a few times.

for (i, c) in enumerate(cnts):
    (x, y, w, h) = cv2.boundingRect(c)
    ar = w / float(h)
    if ar > 2.5 and ar < 4.0:
        if (w > 40 and w < 55) and (h > 10 and h < 20):

            locs.append((x, y, w, h))
locs = sorted(locs, key=lambda x:x[0])
output = []

首先计算矩形,然后选择合适的区域,根据实际任务,这里的基本是一组四个数字。筛选,然后再次排序。

[En]

First calculate the rectangle, and then select the appropriate area, according to the actual task, the basic here is a set of four numbers. Filter, and then sort again.

通过过滤操作我们就只剩下了这个部分,但是这里是分为四个部分得,4000,1234,5678,9010.

计算机视觉项目-银行卡卡号自动识别
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_BINARY | cv2.THRESH_OTSU)[1]
    cv_show('group',group)
    digitCnts,hierarchy = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
    digitCnts = contours.sort_contours(digitCnts,
        method="left-to-right")[0]

遍历四个位置,然后再次对图像进行预处理,包括灰度、二值化、轮廓、排序,然后从四个数字中找到一个进行显示。

[En]

Traverse the four places, and then preprocess the image again, including grayscale, binary, outline, sorting, and then find one of the four numbers to display.

计算机视觉项目-银行卡卡号自动识别
计算机视觉项目-银行卡卡号自动识别
    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)))

在经历了这些数字之后,我们必须与模板进行比较,得出最好的结果。

[En]

After going through these numbers, we have to make a comparison with the template and come up with the best result.

最后,我们打印出原始图像中的分数:

[En]

Finally, we print out the score in the original image:

计算机视觉项目-银行卡卡号自动识别
太棒了!这就是我们银行卡识别项目的全部内容,为了巩固这个知识,我们做了后期的车牌识别,与这个操作大体一致。我们稍后会更新它。
[En]

Perfect! This is all the contents of our bank card recognition project, and in order to consolidate this knowledge, we did a later stage of license plate recognition to be roughly consistent with this operation. We will update it later.

计算机视觉项目-银行卡卡号自动识别

🔎 支持:🎁🎁🎁 如果觉得博主的文章还不错或者您用得到的话,可以免费的关注一下博主,如果三连收藏支持就更好啦!这就是给予我最大的支持!

Original: https://blog.csdn.net/m0_37623374/article/details/125452464
Author: 吃猫的鱼python
Title: 计算机视觉项目-银行卡卡号自动识别

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6245/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部