python字符分割垂直投影法_opencv python 银行卡号垂直投影法字符分割

文章目录

前言

在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.读入数据

代码如下(示例):

?i=2021011523135161.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ3NTY2NDE2,size_16,color_FFFFFF,t_70#pic_center

银行卡照片来源于网上

3.代码实现结果

?i=20210115232422460.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ3NTY2NDE2,size_16,color_FFFFFF,t_70#pic_center

总结

垂直投影法就是利用二值化后的照片的黑白间隔来分离出字符。

鸣谢

https://blog.csdn.net/weixin_43988887/article/details/90214840

标签:temp,img,width,python,cv2,银行卡,投影,opencv,range

来源: https://blog.csdn.net/qq_47566416/article/details/112689480


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部