前言
当我们分割银行卡字符的时候,会遇到银行卡照片过于模糊,而且我们的算法无法提取,这样的情况下我们就会用到我们自己去手动寻找银行卡卡号的位置。
提示:以下是本篇文章正文内容,下面案例可供参考
一、代码
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 =

最低0.47元/天 解锁文章

1449

被折叠的 条评论
为什么被折叠?



