opencv-python 银行卡号手动分割


前言

当我们分割银行卡字符的时候,会遇到银行卡照片过于模糊,而且我们的算法无法提取,这样的情况下我们就会用到我们自己去手动寻找银行卡卡号的位置。


提示:以下是本篇文章正文内容,下面案例可供参考

一、代码

import cv2 as cv;
import numpy as np


def shoudong(img1):
    roi = cv.selectROI(windowName="roi", img=img1, showCrosshair=True, fromCenter=False)
    x, y, w, h = roi
    if roi != (0, 0, 0, 0):
        crop = img[y:y + h, x:x + w]
        cv.imshow('crop', crop)
    return image_process(crop)


def image_process(img):
    # img = cv.imread(file_path, 0)
    blur = cv.GaussianBlur(img, (3, 3), 0)     # 高斯模糊
    gray = cv.cvtColor(blur, cv.COLOR_BGR2GRAY)  # 将图像变为灰路图像
    ret, binary = cv.threshold(gray, 50, 255, cv.THRESH_BINARY)       # 二值化

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


def num_split(img):

    kernel = np.ones((1, 50), np.uint8)
    erosion = cv.erode(img, kernel)         # 膨胀
    dilation = cv.dilate(erosion, kernel)      # 腐蚀
    height, width = dilation.shape
    v = [0] * width
    z = [0] * height
    a = 0

    # 垂直投影:统计并存储每一列的黑点数
    for x in range(0, width):
        for y in range(0, height):
            if img[y, x] == 255:
                continue
            else:
                a = a + 1
        v[x] = a
        a = 0

    # 创建空白图片,绘制垂直投影图
    l = len(v)
    emptyImage = np.full((height, width), 255, dtype=np.uint8)
    for x in range(0, width):
        for y in range(0, v[x]):
            emptyImage[y, x] = 0

    #分割字符
    Position = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值