python简单实现图片文字分割
本文实例为大家分享了python简单实现图片文字分割的具体代码,供大家参考,具体内容如下
原图:
文章图片
【python简单实现图片文字分割】图片预处理:图片二值化以及图片降噪处理。
# 图片二值化def binarization(img,threshold):#图片二值化操作width,height=img.sizeim_new = img.copy()for i in range(width):for j in range(height):a = img.getpixel((i, j))aa = 0.30 * a[0] + 0.59 * a[1] + 0.11 * a[2]if (aa <= threshold):im_new.putpixel((i, j), (0, 0, 0))else:im_new.putpixel((i, j), (255, 255, 255))# im_new.show()# 显示图像return im_new
# 图片降噪处理def clear_noise(img):# 图片降噪处理x, y = img.width, img.heightfor i in range(x-1):for j in range(y-1):if sum_9_region(img, i, j) < 600:# 改变像素点颜色,白色img.putpixel((i, j), (255,255,255))# img = np.array(img)## cv2.imwrite('handle_two.png', img)## img = Image.open('handle_two.png')img.show()return img# 获取田字格内当前像素点的像素值def sum_9_region(img, x, y):"""田字格"""# 获取当前像素点的像素值a1 = img.getpixel((x - 1, y - 1))[0]a2 = img.getpixel((x - 1, y))[0]a3 = img.getpixel((x - 1, y+1 ))[0]a4 = img.getpixel((x, y - 1))[0]a5 = img.getpixel((x, y))[0]a6 = img.getpixel((x, y+1 ))[0]a7 = img.getpixel((x+1 , y - 1))[0]a8 = img.getpixel((x+1 , y))[0]a9 = img.getpixel((x+1 , y+1))[0]width = img.widthheight = img.heightif a5 == 255:# 如果当前点为白色区域,则不统计邻域值return 2550if y == 0:# 第一行if x == 0:# 左上顶点,4邻域# 中心点旁边3个点sum_1 = a5 + a6 + a8 + a9return 4*255 - sum_1elif x == width - 1:# 右上顶点sum_2 = a5 + a6 + a2 + a3return 4*255 - sum_2else:# 最上非顶点,6邻域sum_3 = a2 + a3+ a5 + a6 + a8 + a9return 6*255 - sum_3elif y == height - 1:# 最下面一行if x == 0:# 左下顶点# 中心点旁边3个点sum_4 = a5 + a8 + a7 + a4return 4*255 - sum_4elif x == width - 1:# 右下顶点sum_5 = a5 + a4 + a2 + a1return 4*255 - sum_5else:# 最下非顶点,6邻域sum_6 = a5+ a2 + a8 + a4 +a1 + a7return 6*255 - sum_6else:# y不在边界if x == 0:# 左边非顶点sum_7 = a4 + a5 + a6 + a7 + a8 + a9return 6*255 - sum_7elif x == width - 1:# 右边非顶点sum_8 = a4 + a5 + a6 + a1 + a2 + a3return 6*255 - sum_8else:# 具备9领域条件的sum_9 = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9return 9*255 - sum_9
经过二值化和降噪后得到的图片
文章图片
对图片进行水平投影与垂直投影:
# 传入二值化后的图片进行垂直投影def vertical(img):"""传入二值化后的图片进行垂直投影"""pixdata = https://www.it610.com/article/img.load()w,h = img.sizever_list = []# 开始投影for x in range(w):black = 0for y in range(h):if pixdata[x,y][0] == 0:black += 1ver_list.append(black)# 判断边界l,r = 0,0flag = Falset=0#判断分割数量cuts = []for i,count in enumerate(ver_list):# 阈值这里为0if flag is False and count> 0:l = iflag = Trueif flag and count == 0:r = i-1flag = Falsecuts.append((l,r))#记录边界点t += 1#print(t)return cuts,t# 传入二值化后的图片进行水平投影def horizontal(img):"""传入二值化后的图片进行水平投影"""pixdata = https://www.it610.com/article/img.load()w,h = img.sizever_list = []# 开始投影for y in range(h):black = 0for x in range(w):if pixdata[x,y][0] == 0:black += 1ver_list.append(black)# 判断边界l,r = 0,0flag = False# 分割区域数t=0cuts = []for i,count in enumerate(ver_list):# 阈值这里为0if flag is False and count> 0:l = iflag = Trueif flag and count == 0:r = i-1flag = Falsecuts.append((l,r))t += 1return cuts,t
这两段代码目的主要是为了分割得到水平和垂直位置的每个字所占的大小,接下来就是对预处理好的图片文字进行分割。
# 创建获得图片路径并处理图片函数def get_im_path():OpenFile = tk.Tk()#创建新窗口OpenFile.withdraw()file_path = filedialog.askopenfilename()im = Image.open(file_path)# 阈值th = getthreshold(im) - 16print(th)# 原图直接二值化im_new1 = binarization(im, th)im_new1.show()# 直方图均衡化im1 = his_bal(im)im1.show()im_new_np = np.array(his_bal(im))th1 = getthreshold(im1) - 16print(th1)# 二值化im_new = binarization(im1, th1)# 降噪im_new_cn = clear_noise(im_new)height = im_new_cn.size[1]print(height)# 算出水平投影和垂直投影的数值v, vt = vertical(im_new1)h, ht = horizontal(im_new1)# 算出分割区域a = []for i in range(vt):a.append((v[i][0], 0, v[i][1], height))print(a)im_new.show()# 直方图均衡化后再二值化# 切割for i, n in enumerate(a, 1):temp = im_new_cn.crop(n)# 调用crop函数进行切割temp.show()temp.save("c/%s.png" % i)
至此大概就完成了。
接下来是文件的全部代码:
import numpy as npfrom PIL import Imageimport queueimportmatplotlib.pyplot as pltimporttkinter as tkfrom tkinter import filedialog#导入文件对话框函数库window = tk.Tk()window.title('图片选择界面')window.geometry('400x100')var = tk.StringVar()# 创建获得图片路径并处理图片函数def get_im_path():OpenFile = tk.Tk()#创建新窗口OpenFile.withdraw()file_path = filedialog.askopenfilename()im = Image.open(file_path)# 阈值th = getthreshold(im) - 16print(th)# 原图直接二值化im_new1 = binarization(im, th)im_new1.show()# 直方图均衡化im1 = his_bal(im)im1.show()im_new_np = np.array(his_bal(im))th1 = getthreshold(im1) - 16print(th1)# 二值化im_new = binarization(im1, th1)# 降噪im_new_cn = clear_noise(im_new)height = im_new_cn.size[1]print(height)# 算出水平投影和垂直投影的数值v, vt = vertical(im_new1)h, ht = horizontal(im_new1)# 算出分割区域a = []for i in range(vt):a.append((v[i][0], 0, v[i][1], height))print(a)im_new.show()# 直方图均衡化后再二值化# 切割for i, n in enumerate(a, 1):temp = im_new_cn.crop(n)# 调用crop函数进行切割temp.show()temp.save("c/%s.png" % i)# 传入二值化后的图片进行垂直投影def vertical(img):"""传入二值化后的图片进行垂直投影"""pixdata = https://www.it610.com/article/img.load()w,h = img.sizever_list = []# 开始投影for x in range(w):black = 0for y in range(h):if pixdata[x,y][0] == 0:black += 1ver_list.append(black)# 判断边界l,r = 0,0flag = Falset=0#判断分割数量cuts = []for i,count in enumerate(ver_list):# 阈值这里为0if flag is False and count> 0:l = iflag = Trueif flag and count == 0:r = i-1flag = Falsecuts.append((l,r))#记录边界点t += 1#print(t)return cuts,t# 传入二值化后的图片进行水平投影def horizontal(img):"""传入二值化后的图片进行水平投影"""pixdata = https://www.it610.com/article/img.load()w,h = img.sizever_list = []# 开始投影for y in range(h):black = 0for x in range(w):if pixdata[x,y][0] == 0:black += 1ver_list.append(black)# 判断边界l,r = 0,0flag = False# 分割区域数t=0cuts = []for i,count in enumerate(ver_list):# 阈值这里为0if flag is False and count> 0:l = iflag = Trueif flag and count == 0:r = i-1flag = Falsecuts.append((l,r))t += 1return cuts,t# 获得阈值算出平均像素def getthreshold(im):#获得阈值 算出平均像素wid, hei = im.sizehist = [0] * 256th = 0for i in range(wid):for j in range(hei):gray = int(0.3 * im.getpixel((i, j))[0] + 0.59 * im.getpixel((i, j))[1] + 0.11 * im.getpixel((i, j))[2])th = gray + thhist[gray] += 1threshold = int(th/(wid*hei))return threshold# 直方图均衡化 提高对比度def his_bal(im):#直方图均衡化 提高对比度# 统计灰度直方图im_new = im.copy()wid, hei = im.sizehist = [0] * 256for i in range(wid):for j in range(hei):gray = int(0.3*im.getpixel((i,j))[0]+0.59*im.getpixel((i,j))[1]+0.11*im.getpixel((i,j))[2])hist[gray] += 1# 计算累积分布函数cdf = [0] * 256for i in range(256):if i == 0:cdf[i] = hist[i]else:cdf[i] = cdf[i - 1] + hist[i]# 用累积分布函数计算输出灰度映射函数LUTnew_gray = [0] * 256for i in range(256):new_gray[i] = int(cdf[i] / (wid * hei) * 255 + 0.5)# 遍历原图像,通过LUT逐点计算新图像对应的像素值for i in range(wid):for j in range(hei):gray = int(0.3*im.getpixel((i,j))[0]+0.59*im.getpixel((i,j))[1]+0.11*im.getpixel((i,j))[2])im_new.putpixel((i, j), new_gray[gray])return im_new# 图片二值化def binarization(img,threshold):#图片二值化操作width,height=img.sizeim_new = img.copy()for i in range(width):for j in range(height):a = img.getpixel((i, j))aa = 0.30 * a[0] + 0.59 * a[1] + 0.11 * a[2]if (aa <= threshold):im_new.putpixel((i, j), (0, 0, 0))else:im_new.putpixel((i, j), (255, 255, 255))# im_new.show()# 显示图像return im_new# 图片降噪处理def clear_noise(img):# 图片降噪处理x, y = img.width, img.heightfor i in range(x-1):for j in range(y-1):if sum_9_region(img, i, j) < 600:# 改变像素点颜色,白色img.putpixel((i, j), (255,255,255))# img = np.array(img)## cv2.imwrite('handle_two.png', img)## img = Image.open('handle_two.png')img.show()return img# 获取田字格内当前像素点的像素值def sum_9_region(img, x, y):"""田字格"""# 获取当前像素点的像素值a1 = img.getpixel((x - 1, y - 1))[0]a2 = img.getpixel((x - 1, y))[0]a3 = img.getpixel((x - 1, y+1 ))[0]a4 = img.getpixel((x, y - 1))[0]a5 = img.getpixel((x, y))[0]a6 = img.getpixel((x, y+1 ))[0]a7 = img.getpixel((x+1 , y - 1))[0]a8 = img.getpixel((x+1 , y))[0]a9 = img.getpixel((x+1 , y+1))[0]width = img.widthheight = img.heightif a5 == 255:# 如果当前点为白色区域,则不统计邻域值return 2550if y == 0:# 第一行if x == 0:# 左上顶点,4邻域# 中心点旁边3个点sum_1 = a5 + a6 + a8 + a9return 4*255 - sum_1elif x == width - 1:# 右上顶点sum_2 = a5 + a6 + a2 + a3return 4*255 - sum_2else:# 最上非顶点,6邻域sum_3 = a2 + a3+ a5 + a6 + a8 + a9return 6*255 - sum_3elif y == height - 1:# 最下面一行if x == 0:# 左下顶点# 中心点旁边3个点sum_4 = a5 + a8 + a7 + a4return 4*255 - sum_4elif x == width - 1:# 右下顶点sum_5 = a5 + a4 + a2 + a1return 4*255 - sum_5else:# 最下非顶点,6邻域sum_6 = a5+ a2 + a8 + a4 +a1 + a7return 6*255 - sum_6else:# y不在边界if x == 0:# 左边非顶点sum_7 = a4 + a5 + a6 + a7 + a8 + a9return 6*255 - sum_7elif x == width - 1:# 右边非顶点sum_8 = a4 + a5 + a6 + a1 + a2 + a3return 6*255 - sum_8else:# 具备9领域条件的sum_9 = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9return 9*255 - sum_9btn_Open = tk.Button(window,text='打开图像',# 显示在按钮上的文字width=15, height=2,command=get_im_path)# 点击按钮式执行的命令btn_Open.pack()# 运行整体窗口window.mainloop()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 科学养胃,别被忽悠,其实真的很简单
- 逻辑回归的理解与python示例
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum