opencv的实战学习之信用卡识别(四)

效果展示

在这里插入图片描述
在这里插入图片描述

流程介绍

  • 先把图片进行处理,提取数字部分
  • 将四个数字分割,并进行模板匹配
  • 处理数字和区域,标注在原图上

步骤

进行图片处理

  • 图片太大,需要进行缩放,等会标注的时候需要放大回来。
  • 进行一次顶帽操作, 扩大特征
  • 用sobel进行边缘检测,这里用的是x方向的检测,然后进行归一化处理,把值控制在255内
sobelX = cv.Sobel(top_hat, cv.CV_32F, 1, 0)
  • cv_32F目标是32位有符号浮点数,因为sobel算子是用的右边减去左边,有可能为负数,这样保留正负数,再进行绝对值操作
sobelX = np.absolute(sobelX)
(min, max) = (np.min(top_hat), np.max(top_hat))
sobelX = (255 * ((sobelX - min)/ (max - min)))
sobelX = sobelX.astype('uint8')
  • 进行一步闭合操作,去除噪点
  • 进行二值化操作
  • 进行闭操作,连接成块
  • 进行边缘检测
  • 过滤选择区域
  • 进行数字区域分割提取
  • 进行数字内容识别
def find_num(ori_path):
    card_path = ori_path

    card_img = cv.imread(card_path)
    card_img = cv.resize(card_img, None, fx=0.7, fy=0.7)
    # 灰度化
    card_gray = cv.cvtColor(card_img, cv.COLOR_BGR2GRAY)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
    rectKernel = cv.getStructuringElement(cv.MORPH_RECT, (13, 3))
    top_hat = cv.morphologyEx(card_gray, cv.MORPH_TOPHAT, kernel)
    # 算子sobel
    sobelX = cv.Sobel(top_hat, cv.CV_32F, 1, 0)
    
    sobelX = np.absolute(sobelX)
    (min, max) = (np.min(top_hat), np.max(top_hat))
    sobelX = (255 * ((sobelX - min)/ (max - min)))
    sobelX = sobelX.astype('uint8')
    # 二值化
    card_close = cv.morphologyEx(sobelX, cv.MORPH_CLOSE, rectKernel)
    ret, threshold = cv.threshold(card_close, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)

    card_gray = cv.morphologyEx(threshold, cv.MORPH_CLOSE, kernel, iterations=1)

    contours, hierachy = cv.findContours(card_gray, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    # cv.drawContours(card_img, contours, -1, (0, 255, 0), 4)
    list = []
    
    for cont in contours:
        # 画矩形
        x, y, w, h = cv.boundingRect(cont)
        rate = w/h
        if 2.7 < rate < 3.5:
            if (50 < w < 95) and (10 < h < 40):
                # rect = cv.rectangle(card_img, (x, y), (x+w, y+h), (0, 255, 0), 1)
                # cv.imshow('card1', card_img[y: y + h, x: x + w])
                list.append(([x, y, w, h], card_img[y: y + h, x: x + w]))
    # 从左到右排序
    list = sorted(list, key= lambda a: a[0][0])
    num_list = []
    for cont, img in list:
        four_list = ""
        img1 = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        ret, img1 = cv.threshold(img1, 0, 255,  cv.THRESH_BINARY | cv.THRESH_OTSU)
        contours1, hierachy = cv.findContours(img1, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
        # cv.drawContours(img, contours1, -1, (0, 255, 0), 1)
        temp_list = []
        for cont1 in contours1:
            # 绘制矩形
            x, y, w, h = cv.boundingRect(cont1)
            rect = cv.rectangle(img1, (x, y), (x+w, y+h), (0, 255, 0), 1)
            
            rect1 = img1[y: y + h , x : x + w ]
            temp_list.append(([x, y, w, h], rect1))
        temp_list = sorted(temp_list, key= lambda a: a[0][0])
        for i, img2 in temp_list:
            four_list += (match_img(img2))
        num_list.append(four_list)
    print(num_list)
    return list, num_list

模板的处理

  • 直接用常规操作进行分割
  • 写入本地数据
def split_template():
    reference_path = './data/reference.png'
    img_base = cv.imread(reference_path)
    img_gray = cv.cvtColor(img_base, cv.COLOR_BGR2GRAY)
    ret, threshold = cv.threshold(img_gray, 0, 255, cv.THRESH_BINARY_INV)

    contours, hierachy = cv.findContours(threshold, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    for i, cont in enumerate(contours):
        x, y, w, h = cv.boundingRect(cont)
        cv.imwrite('template/%s.png'%i, threshold[y- 5: y + h + 5, x - 5: x + w + 5])

匹配模板

  • 因为上面导出的图片名称为
    在这里插入图片描述
  • 所以数字应该用 9- 下标,应该注意下。
import os
def match_img(img):
    max = 0
    max_index = 0
    for i,name in enumerate(os.listdir("./template")):
        img_bg = cv.imread('./template/%s'%name, 0)
        img = cv.resize(img, (img_bg.shape[1], img_bg.shape[0]))
        result = cv.matchTemplate(img_bg, img, cv.TM_CCOEFF_NORMED)
        if result > max:
            max = result
            max_index = 9 - i
    return str(max_index)

展示匹配效果

  • 这里需要传入原始图片, 图片列表, 数字列表
  • 遍历标注即可
def draw_card(img, list, num_list):
    # 传入坐标list, 数字list (list, numList)
    for i, cont in enumerate(list):
        x, y, w, h = cont[0]
        x = int(x/0.7); y = int(y/0.7); w = int(w/0.7); h = int(h/0.7)
        print(x, y, w, h)
        cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
        num_str = num_list[i]
        cv.putText(img, num_str, (x, y - 10), cv.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2, cv.LINE_AA)
    cv.imshow('card', img)
    cv.waitKey(0)
    cv.destroyAllWindows()

调用主函数

def main():
    ori_path = './data/card3.png'
    split_template()
    list, num_list = find_num(ori_path)
    draw_card(cv.imread(ori_path), list, num_list)
main()

效果

在这里插入图片描述

缺陷

在这里插入图片描述

  • 这里有张银色的会有反光效果,处理起来不是很好,而且下面的英文也和数字的比例很像。
  • 有找到处理方案的同学,可以留言评论

资源获取

点击下载

完整代码

import numpy as np
import cv2 as cv
from PIL import Image

import os
def match_img(img):
    max = 0
    max_index = 0
    for i,name in enumerate(os.listdir("./template")):
        img_bg = cv.imread('./template/%s'%name, 0)
        img = cv.resize(img, (img_bg.shape[1], img_bg.shape[0]))
        result = cv.matchTemplate(img_bg, img, cv.TM_CCOEFF_NORMED)
        if result > max:
            max = result
            max_index = 9 - i
    
    return str(max_index)
def draw_card(img, list, num_list):
    # 传入坐标list, 数字list (list, numList)
    for i, cont in enumerate(list):
        x, y, w, h = cont[0]
        x = int(x/0.7); y = int(y/0.7); w = int(w/0.7); h = int(h/0.7)
        print(x, y, w, h)
        cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
        num_str = num_list[i]
        cv.putText(img, num_str, (x, y - 10), cv.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2, cv.LINE_AA)
    cv.imshow('card', img)
    cv.waitKey(0)
    cv.destroyAllWindows()

def find_num(ori_path):
    card_path = ori_path

    card_img = cv.imread(card_path)
    card_img = cv.resize(card_img, None, fx=0.7, fy=0.7)
    # 灰度化
    card_gray = cv.cvtColor(card_img, cv.COLOR_BGR2GRAY)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
    rectKernel = cv.getStructuringElement(cv.MORPH_RECT, (13, 3))
    top_hat = cv.morphologyEx(card_gray, cv.MORPH_TOPHAT, kernel)
    # 算子sobel
    sobelX = cv.Sobel(top_hat, cv.CV_32F, 1, 0)
    sobelX = np.absolute(sobelX)
    (min, max) = (np.min(top_hat), np.max(top_hat))
    sobelX = (255 * ((sobelX - min)/ (max - min)))
    sobelX = sobelX.astype('uint8')
    # 二值化
    card_close = cv.morphologyEx(sobelX, cv.MORPH_CLOSE, rectKernel)
    ret, threshold = cv.threshold(card_close, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)

    card_gray = cv.morphologyEx(threshold, cv.MORPH_CLOSE, kernel, iterations=1)

    contours, hierachy = cv.findContours(card_gray, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    # cv.drawContours(card_img, contours, -1, (0, 255, 0), 4)
    list = []
    
    for cont in contours:
        # 画矩形
        x, y, w, h = cv.boundingRect(cont)
        rate = w/h
        if 2.7 < rate < 3.5:
            if (50 < w < 95) and (10 < h < 40):
                # rect = cv.rectangle(card_img, (x, y), (x+w, y+h), (0, 255, 0), 1)
                # cv.imshow('card1', card_img[y: y + h, x: x + w])
                list.append(([x, y, w, h], card_img[y: y + h, x: x + w]))
    # 从左到右排序
    list = sorted(list, key= lambda a: a[0][0])
    num_list = []
    for cont, img in list:
        four_list = ""
        img1 = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        ret, img1 = cv.threshold(img1, 0, 255,  cv.THRESH_BINARY | cv.THRESH_OTSU)
        contours1, hierachy = cv.findContours(img1, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
        # cv.drawContours(img, contours1, -1, (0, 255, 0), 1)
        temp_list = []
        for cont1 in contours1:
            # 绘制矩形
            x, y, w, h = cv.boundingRect(cont1)
            rect = cv.rectangle(img1, (x, y), (x+w, y+h), (0, 255, 0), 1)
            
            rect1 = img1[y: y + h , x : x + w ]
            temp_list.append(([x, y, w, h], rect1))
        temp_list = sorted(temp_list, key= lambda a: a[0][0])
        for i, img2 in temp_list:
            four_list += (match_img(img2))
        num_list.append(four_list)
    print(num_list)
    return list, num_list

def split_template():
    reference_path = './data/reference.png'
    img_base = cv.imread(reference_path)
    img_gray = cv.cvtColor(img_base, cv.COLOR_BGR2GRAY)
    ret, threshold = cv.threshold(img_gray, 0, 255, cv.THRESH_BINARY_INV)

    contours, hierachy = cv.findContours(threshold, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    for i, cont in enumerate(contours):
        x, y, w, h = cv.boundingRect(cont)
        cv.imwrite('template/%s.png'%i, threshold[y- 5: y + h + 5, x - 5: x + w + 5])
def main():
    ori_path = './data/card4.png'
    split_template()
    list, num_list = find_num(ori_path)
    draw_card(cv.imread(ori_path), list, num_list)
main()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东哥爱编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值