python实现图片中文字分割效果
本文实例为大家分享了python实现图片中文字分割的具体代码,供大家参考,具体内容如下
1、原始图片(包含数字):
结果图:
2、原始图片(包含文字):
结果图:
3、代码如下:
import cv2 import numpy as np path = 'test.jpg' root = 'output\\' # 图像resize dsize = 36 img = cv2.imread(path) data = np.array(img) height = data.shape[0] width = data.shape[1] # 设置最小的文字像素高度 min_val = 10 start_i = -1 end_i = -1 # 存放每行的起止坐标 rowinfo = [] # 行分割 for i in range(height): # 行中有字相关信息 if (not data[i].all()): end_i = i if(start_i < 0): start_i = i pass # 行中无字相关信息 elif (data[i].all() and start_i >= 0): if(end_i - start_i >= min_val): rowinfo.append((start_i, end_i)) pass start_i, end_i = -1, -1 print(rowinfo) # 列分割 start_j = -1 end_j = -1 # 最小文字像素宽度 min_val_word = 5 # 分割后保存编号 number = 0 for start, end in rowinfo: for j in range(width): # 列中有字相关信息 if(not data[start: end, j].all()): end_j = j if(start_j < 0): start_j = j pass # 列中无字信息 elif(data[start: end, j].all() and start_j >= 0): if(end_j - start_j >= min_val_word): img = data[start:end, start_j: end_j] im2save = cv2.resize(img, (dsize, dsize)) #归一化处理 cv2.imwrite(root + '%d.jpg' % number, im2save) number += 1 pass start_j, end_j = -1, -1
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。