python 3.6
python-opencv 3.4.0
对图片进行水平方向投影,得到每一行文字的起止线
对每一行文字进行竖直方向投影,若有连续的像素满足条件,则为一个字,进行分割
import cv2
import numpy as np
path = 'd:\\testdata\\test\\bin1.png'
root = 'd:\\testdata\\test\\'
dsize = 28 #归一化处理的图像大小
img = cv2.imread(path)
data = np.array(img)
len_x = data.shape[0]
len_y = data.shape[1]
min_val = 10 #设置最小的文字像素高度,防止切分噪音字符
start_i = -1
end_i = -1
rowPairs = [] #存放每行的起止坐标
#行分割
for i in range(len_x):
if(not data[i].all() and start_i < 0):
start_i = i
elif(not data[i].all()):
end_i = i
elif (data[i].all() and start_i >= 0):
#print(end_i - start_i)
if(end_i - start_i >= min_val):
rowPairs.append((start_i, end_i))
start_i, end_i = -1, -1
#print(rowPairs)
#列分割
start_j = -1
end_j = -1
min_val_word = 5 #最小文字像素长度
number = 0 #分割后保存编号
for start, end in rowPairs:
for j in range(len_y):
if(not data[start: end, j].all() and start_j < 0):
start_j = j
elif(not data[start: end, j].all()):
end_j = j
elif(data[start: end, j].all() and start_j >= 0):
if(end_j - start_j >= min_val_word):
#print(end_j - start_j)
tmp = data[start:end, start_j: end_j]
im2save = cv2.resize(tmp, (dsize,dsize)) #归一化处理
cv2.imwrite(root + '%d.png' % number, im2save)
number += 1
#print("%d pic" % number)
start_j, end_j = -1, -1
效果还行