利用OpenCV实现银行卡号数字识别(一)

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

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

1、获取银行卡号码序列区域(效果不好)

import cv2
import numpy as np


def seeImage(windowName, imageData):
    """
        显示图像
    :param windowName:  窗体名称
    :param imageData:   图像数据
    """
    cv2.imshow(windowName, imageData)
    cv2.waitKey(0)
    # cv2.destroyAllWindows()


def drawRectangle(image):
    """
        查看轮廓的宽、高比率
    :param image: 图像
    """
    contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    imageOne = np.ones((image.shape[0], image.shape[1], 3), dtype=np.uint8) * 255
    font = cv2.FONT_HERSHEY_SIMPLEX  # 字体样式
    for index in range(len(contours)):
        x, y, w, h = cv2.boundingRect(contours[index])
        ratioW_H = round(float(w) / h, 2)
        Area = w * h
        txtInfo = str(Area) + "_" + str(ratioW_H)
        cv2.putText(imageOne, txtInfo, (x, y), font, 0.4, (255, 255, 0), 1)
        imageOne = cv2.rectangle(imageOne, (x, y), (x + w, y + h), (0, 0, 255), 1)
        print(txtInfo)
    seeImage("imageOne", imageOne)


cardImage = cv2.imread("bank_card4.jpg")  # 读入银行卡图像
imageGrayImage = cv2.cvtColor(cardImage, cv2.COLOR_BGR2GRAY)  # 将图像转为灰度图像
imagResize = cv2.resize(imageGrayImage, (0, 0), fx=4, fy=4)  # 将原始图像宽与高缩放为原先的3# ret,imageThresh = cv2.threshold(imageGrayImage, 200, 255, cv2.THRESH_BINARY)
# seeImage("imageThresh",imageThresh)
imageCopy = imagResize.copy()
adaptThreImage = cv2.adaptiveThreshold(imageCopy, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 13, 3)
# seeImage("adaptThreImage", adaptThreImage)
contours, herarchy = cv2.findContours(adaptThreImage, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for index in range(len(contours)):
    if cv2.contourArea(contours[index]) < 400:
        adaptThreImage = cv2.drawContours(adaptThreImage, contours, index, (0, 0, 0), -1)
seeImage("adaptThreImage", adaptThreImage)
# drawRectangle(adaptThreImage)

"""利用轮廓外接矩形面积和宽高比去除噪声"""
contours, herarchy = cv2.findContours(adaptThreImage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for index in range(len(contours)):
    x, y, w, h = cv2.boundingRect(contours[index])
    aspectStadio = float(w) / h
    Area = w * h
    if Area < 5000 or Area > 6000:
        adaptThreImage = cv2.drawContours(adaptThreImage, contours, index, (0, 0, 0), -1)
    else:
        if aspectStadio < 0.6 or aspectStadio > 0.75:
            adaptThreImage = cv2.drawContours(adaptThreImage, contours, index, (0, 0, 0), -1)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (95, 21))
dilate = cv2.dilate(adaptThreImage, kernel, 1)
seeImage("dilate", dilate)
drawRectangle(dilate)

显示效果如下所示:
在这里插入图片描述

2、获取银行卡号码序列区域(效果好)

import cv2
import numpy as np

# 矩形: MORPH_RECT
# 交叉形: MORPH_CROSS
# 椭圆形: MORPH_ELLIPSE


def seeImage(windowName, imageData):
    """
        显示图像
    :param windowName:  窗体名称
    :param imageData:   图像数据
    """
    cv2.imshow(windowName, imageData)
    cv2.waitKey
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值