关于Python直方图均衡化

原地址:https://blog.csdn.net/sunmc1204953974/article/details/50606395


from PIL import Image

from pylab import *

#读取图像到数组中,并灰度化
im = array(Image.open('cat.jpg').convert('L'))

print(im.shape)

#绘制原始直方图
subplot(231)

hist(im.flatten(),5)

#计算图像直方图(每个bins数组的区间值对应一个imhist数组中的强度值)
#该函数返回值imhist表示每个区间的强度,bins表示坐标轴
imhist,bins = histogram(im.flatten(),5,normed=True)
print(imhist.shape)
print(imhist.sum())
print(bins.shape)
print("imhist",imhist)
print("bins",bins)


#计算累积分布函数
#就是概率分布的积分
cdf = imhist.cumsum()

print("cdf",cdf[-1])

#累计函数归一化(由01变换至0~255cdf = cdf*255/cdf[-1]

#绘制累计分布函数
subplot(232)

plot(bins[1:6],cdf)

#依次对每一个灰度图像素值(强度值)使用cdf进行线性插值,计算其新的强度值
#interpxxpyp) 输入原函数的一系列点(xpyp),使用线性插值方法模拟函数并根据这个函数计算fxim2 = interp(im.flatten(),bins[:5],cdf)

#将压平的图像数组重新变成二维数组
im2 = im2.reshape(im.shape)

# 显示均衡化之后的直方图图像
subplot(233)

hist(im2.flatten(),5)

#显示原始图像
gray()

subplot(234)

imshow(im)

#显示变换后图像
subplot(236)

imshow(im2)

show()

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值