import numpy as np
from PIL import Image
import queue
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import filedialog
def main():
#获取图片路径
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_new_cn.crop(n) # 调用crop函数进行切割
temp.show()
temp.save("c/%s.png" % i)
#获得阈值 算出平均像素
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 binarization(img,threshold):
width,height=img.size
im_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))
return im_new
#传入二值化后的图片进行垂直投影
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
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 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.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.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_
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
咕噜咕噜
最新推荐文章于 2024-08-19 13:32:52 发布