python+opencv实现信用卡识别

任务:使用opencv利用模板实现对信用卡卡号的识别

模板图像如图:
在这里插入图片描述
使用opencv对图像处理得到图像对应的数字并使用字典存储

def template_process(img):
    temp_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

    # 转换颜色
    ret, temp_img = cv.threshold(temp_img, 127, 255, cv.THRESH_BINARY_INV)
    # show('temp', temp_img)

    # 模板轮廓检测
    contours, hierarchy = cv.findContours(temp_img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

    # 画出轮廓-->在原来的图像上才能画出彩色
    res = cv.drawContours(img, contours, -1, (255, 0, 0), 3)
    show('temp-counteour', res)

    # 对轮廓进行排序
    contours, bounding_loc = sort_contour(contours)

    # 图形与数字匹配
    digitals = {}
    for i in range(len(bounding_loc)):
        x, y, w, h = bounding_loc[i]
        digit_area = temp_img[y:y + h, x:x + w]
        digit_area = cv.resize(digit_area, (70, 120))
        # show("num", digit_area)
        digitals[i] = digit_area
    return digitals

得到数字对应的10个轮廓
处理信用卡流程:

信用卡原图:
在这里插入图片描述
对信用卡进行顶帽操作突出数字区域
在这里插入图片描述
计算水平梯度将4个数字为一组让其更像一个块
在这里插入图片描述
进行闭操作将数字连在一起

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值