python简单实现图⽚⽂字分割本⽂实例为⼤家分享了python简单实现图⽚⽂字分割的具体代码,供⼤家参考,具体内容如下原图:
图⽚预处理:图⽚⼆值化以及图⽚降噪处理。
# 图⽚⼆值化
def binarization(img,threshold):
#图⽚⼆值化操作
width,height=img.size
im_new = py()
for i in range(width):
for j in range(height):
a = pixel((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.height
for 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 = pixel((x - 1, y - 1))[0]
a2 = pixel((x - 1, y))[0]
a3 = pixel((x - 1, y+1 ))[0]
a4 = pixel((x, y - 1))[0]
a5 = pixel((x, y))[0]
a6 = pixel((x, y+1 ))[0]
a7 = pixel((x+1 , y - 1))[0]
a8 = pixel((x+1 , y))[0]
a9 = pixel((x+1 , y+1))[0]
width = img.width
height = img.height
if a5 == 255: # 如果当前点为⽩⾊区域,则不统计邻域值
return 2550
if y == 0: # 第⼀⾏
if x == 0: # 左上顶点,4邻域
# 中⼼点旁边3个点
sum_1 = a5 + a6 + a8 + a9
return 4*255 - sum_1
elif x == width - 1: # 右上顶点
sum_2 = a5 + a6 + a2 + a3
return 4*255 - sum_2
else: # 最上⾮顶点,6邻域
sum_3 = a2 + a3+ a5 + a6 + a8 + a9
return 6*255 - sum_3
高速免费几天elif y == height - 1: # 最下⾯⼀⾏
if x == 0: # 左下顶点
# 中⼼点旁边3个点
sum_4 = a5 + a8 + a7 + a4
return 4*255 - sum_4
elif x == width - 1: # 右下顶点
sum_5 = a5 + a4 + a2 + a1
return 4*255 - sum_5
else: # 最下⾮顶点,6邻域
sum_6 = a5+ a2 + a8 + a4 +a1 + a7
return 6*255 - sum_6
else: # y不在边界
if x == 0: # 左边⾮顶点
sum_7 = a4 + a5 + a6 + a7 + a8 + a9
return 6*255 - sum_7
elif x == width - 1: # 右边⾮顶点
sum_8 = a4 + a5 + a6 + a1 + a2 + a3
return 6*255 - sum_8
else: # 具备9领域条件的
sum_9 = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 return 9*255 - sum_9
经过⼆值化和降噪后得到的图⽚
# 传⼊⼆值化后的图⽚进⾏垂直投影
def vertical(img):
"""传⼊⼆值化后的图⽚进⾏垂直投影"""
pixdata = img.load()
w,h = img.size
ver_list = []
# 开始投影
for x in range(w):
black = 0
for y in range(h):
if pixdata[x,y][0] == 0:
black += 1
ver_list.append(black)
# 判断边界
l,r = 0,0
flag = False
t=0#判断分割数量
cuts = []
for i,count in enumerate(ver_list):
# 阈值这⾥为0
if flag is False and count > 0:
l = i
flag = True
if flag and count == 0:
r = i-1
flag = False
cuts.append((l,r))#记录边界点
t += 1
#print(t)
return cuts,t
# 传⼊⼆值化后的图⽚进⾏⽔平投影
def horizontal(img):
"""传⼊⼆值化后的图⽚进⾏⽔平投影"""
pixdata = img.load()
w,h = img.size
ver_list = []
# 开始投影
for y in range(h):
black = 0
for x in range(w):
if pixdata[x,y][0] == 0:
black += 1
ver_list.append(black)
# 判断边界
l,r = 0,0
flag = False
# 分割区域数
t=0
cuts = []
for i,count in enumerate(ver_list):
# 阈值这⾥为0
if flag is False and count > 0:
l = i
flag = True
if flag and count == 0:
r = i-1
flag = False
cuts.append((l,r))
t += 1
return cuts,t
这两段代码⽬的主要是为了分割得到⽔平和垂直位置的每个字所占的⼤⼩,接下来就是对预处理好的图⽚⽂字进⾏分割。# 创建获得图⽚路径并处理图⽚函数
def get_im_path():
OpenFile = tk.Tk()#创建新窗⼝
OpenFile.withdraw()
file_path = filedialog.askopenfilename()
im = Image.open(file_path)
# 阈值
th = getthreshold(im) - 16
print(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) - 16
print(th1)
# ⼆值化
im_new = binarization(im1, th1)
# 降噪
im_new_cn = clear_noise(im_new)
height = im_new_cn.size[1]
2700print(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_p(n) # 调⽤crop函数进⾏切割
temp.show()
temp.save("c/%s.png" % i)
⾄此⼤概就完成了。
接下来是⽂件的全部代码:
import numpy as np
from PIL import Image
import queue
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import filedialog#导⼊⽂件对话框函数库
window = tk.Tk()
window.title('图⽚选择界⾯')
var = tk.StringVar()
# 创建获得图⽚路径并处理图⽚函数
def get_im_path():
OpenFile = tk.Tk()#创建新窗⼝
OpenFile.withdraw()
file_path = filedialog.askopenfilename()
im = Image.open(file_path)
# 阈值
th = getthreshold(im) - 16
print(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) - 16
print(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_p(n) # 调⽤crop函数进⾏切割 temp.show()
temp.save("c/%s.png" % i)
# 传⼊⼆值化后的图⽚进⾏垂直投影
def vertical(img):
"""传⼊⼆值化后的图⽚进⾏垂直投影"""
pixdata = img.load()
w,h = img.size
ver_list = []
# 开始投影
for x in range(w):
black = 0
for y in range(h):
if pixdata[x,y][0] == 0:
black += 1
ver_list.append(black)
# 判断边界
l,r = 0,0
flag = False
t=0#判断分割数量
华为汽车2022款最新款价格cuts = []
for i,count in enumerate(ver_list):
# 阈值这⾥为0奔驰r级amg
if flag is False and count > 0:
l = i
flag = True
if flag and count == 0:
r = i-1
flag = False
cuts.append((l,r))#记录边界点
t += 1
#print(t)
return cuts,t
留学购车# 传⼊⼆值化后的图⽚进⾏⽔平投影
def horizontal(img):
"""传⼊⼆值化后的图⽚进⾏⽔平投影"""
pixdata = img.load()
w,h = img.size
ver_list = []
# 开始投影
for y in range(h):
black = 0
for x in range(w):
if pixdata[x,y][0] == 0:
black += 1
ver_list.append(black)
# 判断边界
l,r = 0,0
flag = False
# 分割区域数
t=0
cuts = []
for i,count in enumerate(ver_list):
# 阈值这⾥为0
if flag is False and count > 0:
l = i
flag = True
if flag and count == 0:
r = i-1
flag = False
cuts.append((l,r))
t += 1
return cuts,t
# 获得阈值算出平均像素
def getthreshold(im):
#获得阈值算出平均像素
wid, hei = im.size
hist = [0] * 256
th = 0
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]) th = gray + th
hist[gray] += 1
threshold = int(th/(wid*hei))
return threshold
# 直⽅图均衡化提⾼对⽐度
def his_bal(im):
#直⽅图均衡化提⾼对⽐度
# 统计灰度直⽅图
im_new = im.copy()
wid, hei = im.size
hist = [0] * 256
for i in range(wid):
for j in range(hei):
gray = int(0.pixel((i,j))[0]+0.pixel((i,j))[1]+0.pixel((i,j))[2])
hist[gray] += 1
# 计算累积分布函数
cdf = [0] * 256
for i in range(256):
if i == 0:
cdf[i] = hist[i]
else:
cdf[i] = cdf[i - 1] + hist[i]
# ⽤累积分布函数计算输出灰度映射函数LUT
new_gray = [0] * 256
for i in range(256):
new_gray[i] = int(cdf[i] / (wid * hei) * 255 + 0.5)
发布评论