opencv python 银行卡号垂直投影法字符分割
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了opencv python 银行卡号垂直投影法字符分割,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2286字,纯文字阅读大概需要4分钟。
内容图文
文章目录
前言
在opencv 和python 作为编程语言的基础上实现银行卡号的识别和分割
提示:以下是本篇文章正文内容,下面案例可供参考
一、银行卡号的识别
[银行卡号的识别所用到的一切都在这篇博客中](https://blog.csdn.net/qq_47566416/article/details/112547470)二、银行卡号的分割
1.引入库
部分代码如下(示例):
import cv2
import numpy as np
def image_process(file_path):
img = cv2.imread(file_path, 0)
blur = cv2.GaussianBlur(img, (3, 3), 0) #高斯模糊
ret, binary = cv2.threshold(blur, 50, 255, cv2.THRESH_BINARY) #二值化
kernel = np.ones((1, 50), np.uint8)
erosion = cv2.erode(binary, kernel) # 膨胀
dilation = cv2.dilate(erosion, kernel) # 腐蚀
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
sp = dilation.shape
x, y, w, h = 0, 0, 0, 0
for i in range(0, len(contours)):
x, y, w, h = cv2.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]
break
return num_split(img)
def num_split(img):
height, width = img.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 = []
Wstart = 0
Wend = 0
W_Start = 0
W_End = 0
v[0], v[len(v)-1] = 0, 0
for j in range(len(v)):
if v[j] > 0 and Wstart == 0:
W_Start = j
Wstart = 1
Wend = 0
if v[j] <= 0 and Wstart == 1:
W_End = j
Wstart = 0
Wend = 1
if Wend == 1:
Position.append([W_Start, 0, W_End, height])
Wend = 0
data = []
for m in range(len(Position)):
temp_img = img[Position[m][1]:Position[m][3], Position[m][0]:Position[m][2]]
h1, w1 = temp_img.shape
if w1 > h1:
return []
temp_img = cv2.resize(temp_img, (16, 16))
h0, w0 = temp_img.shape
temp_data = []
for hx in range(h0):
for wx in range(w0):
temp_data.append(float(temp_img[hx, wx]))
data.append(temp_data)
return data
2.读入数据
代码如下(示例):
银行卡照片来源于网上
3.代码实现结果
总结
垂直投影法就是利用二值化后的照片的黑白间隔来分离出字符。
鸣谢
https://blog.csdn.net/weixin_43988887/article/details/90214840
内容总结
以上是互联网集市为您收集整理的opencv python 银行卡号垂直投影法字符分割全部内容,希望文章能够帮你解决opencv python 银行卡号垂直投影法字符分割所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。