opencv python 银行卡号从银行卡中提取

一、银行卡号提取

先将导入的图片进行高斯模糊,二值化 膨胀 和腐蚀等操作。
再根据银行卡号的样式,从照片中筛选出卡号。

二、代码

1.引入库

代码如下(示例):

import cv2 as cv
import numpy as np


def image_process(file_path):
    img = cv.imread(file_path, 0)
    blur = cv.GaussianBlur(img, (3, 3), 0)     #高斯模糊
    ret, binary = cv.threshold(blur, 50, 255, cv.THRESH_BINARY)       #二值化

    kernel = np.ones((1, 50), np.uint8)
    erosion = cv.erode(binary, kernel)         # 膨胀
    dilation = cv.dilate(erosion, kernel)      # 腐蚀

    contours, hierarchy = cv.findContours(dilation, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    sp = dilation.shape
    for i in range(0, len(contours)):
        x, y, w, h = cv.boundingRect(contours[i])
        if h > sp[0]*0.05 and w > sp[1]*0.5 and y > sp[0]*0.2 and y < sp[0]*0.8 and w/h > 5:
            img = binary[y:y + h, x:x + w]
            cv.imshow("kahao", img)
            break


src = cv.imread("D:\\1111.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input iamge",src)
image_process("D:\\1111.png")
cv.waitKey(0)

cv.destroyAllWindows()

总结

在卡号筛选时,要将筛选的样式设置为高度大于整体图片的0.05,宽度大于照片的0.5,且小于整体照片的0.8,宽比高大于5.这样才能从照片中筛选出银行卡号。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值