# 彩色和灰度图片直方图均衡化

$s_0 = 7\Sigma_{j=0}^0p_r(r_0) = 7 * p_r(r_0) = 1.33$
$s_1 = 7\Sigma_{j=0}^1p_r(r_0) = 7 * p_r(r_0) + 7 * p_r(r_1) = 3.08$
$s_0$四舍五入得1（实际计算机中灰度等级只能是离散的）
$s_1$四舍五入得3。

Lena的灰度图

Lena灰度图做直方图均衡化后

Lena的彩色图

Lena彩色图做直方图均衡化后

def his_equ(img, outfile,level=256,mode='RGB'):
'''

:param img: Image.open打开的文件句柄
:param outfile: 输出文件的文件名
:param level:灰度等级，彩色图是每个通道对应的等级数
:param mode:'rgb'为彩色模式，'gray'为灰度图
:return: 按照输出文件路径保存均衡化之后的图片
'''
if mode == 'RGB' or mode == 'rgb':
r, g, b = [], [], []
width, height = img.size[0], img.size[1]
sum_pix = width * height
for x in range(width):
for y in range(height):
r.append(pix[x, y][0])
g.append(pix[x, y][1])
b.append(pix[x, y][2])
r_c = dict(Counter(r))
g_c = dict(Counter(g))
b_c = dict(Counter(b))
r_p,g_p,b_p = [],[],[]

for i in range(level):
if r_c.has_key(i):
r_p.append(float(r_c[i]) / sum_pix)
else:
r_p.append(0)
if g_c.has_key(i):
g_p.append(float(g_c[i])/sum_pix)
else:
g_p.append(0)
if b_c.has_key(i):
b_p.append(float(b_c[i])/sum_pix)
else:
b_p.append(0)
temp_r,temp_g,temp_b = 0,0,0
for i in range(level):
temp_r += r_p[i]
r_p[i] = int(temp_r * (level-1))
temp_b += b_p[i]
b_p[i] = int(temp_b *(level-1))
temp_g += g_p[i]
g_p[i] = int(temp_g*(level -1))
new_photo = Image.new('RGB',(width,height))
for x in range(width):
for y in range(height):
new_photo.putpixel((x,y),(r_p[pix[x,y][0]],g_p[pix[x,y][1]],b_p[pix[x,y][2]]))
new_photo.save(outfile)
elif mode == 'gray' or mode == 'GRAY':
width, height = img.size[0], img.size[1]
sum_pix = width * height
pb = []
for x in range(width):
for y in range(height):
pb.append(pix[x,y])
pc = dict(Counter(pb))
pb = []
for i in range(level):
if pc.has_key(i):
pb.append(float(pc[i]) / sum_pix)
else:
pb.append(0)
temp = 0
for i in range(level):
temp += pb[i]
pb[i] = int(temp * (level-1))
new_photo = Image.new('L',(width,height))
for x in range(width):
for y in range(height):
new_photo.putpixel((x,y),pb[pix[x,y]])
new_photo.save(outfile)
if __name__ == '__main__':
ppp = Image.open('Lena.jpg','r')
his_equ(ppp,'lena_his.jpg')
posted @ 2017-05-19 23:58 唯心不易 阅读(...) 评论(...) 编辑 收藏