python 灰度直方图_python 对一幅灰度图像进行直方图均衡化

from PIL import Image

from pylab import *

from numpy import *

def histeq(im,nbr_bins = 256):

"""对一幅灰度图像进行直方图均衡化"""

#计算图像的直方图

#在numpy中,也提供了一个计算直方图的函数histogram(),第一个返回的是直方图的统计量,第二个为每个bins的中间值

imhist,bins = histogram(im.flatten(),nbr_bins,normed= True)

cdf = imhist.cumsum() #

cdf = 255.0 * cdf / cdf[-1]

#使用累积分布函数的线性插值,计算新的像素值

im2 = interp(im.flatten(),bins[:-1],cdf)

return im2.reshape(im.shape),cdf

pil_im = Image.open('E:\Python\\fanwei.jpg') #打开原图

pil_im_gray = pil_im.convert('L') #转化为灰度图像

pil_im_gray.show() #显示灰度图像

im = array(Image.open('E:\Python\\fanwei.jpg').convert('L'))

# figure()

# hist(im.flatten(),256)

im2,cdf = histeq(im)

# figure()

# hist(im2.flatten(),256)

# show()

im2 = Image.fromarray(uint8(im2))

im2.show()

# print(cdf)

# plot(cdf)

im2.save("junheng.jpg")

图1:原图的灰度图

20201027102200167.jpg?2020927102237

图2:进行直方图均衡化后的图像

20201027102320304.jpg?2020927102330

图3:原图灰度图的直方图

20201027102353408.png?2020927102528

图4:进行直方图均衡化后的直方图

20201027102508114.png?2020927102637

图5:灰度变换函数

20201027102736579.png?202092710299

以上就是python 对一幅灰度图像进行直方图均衡化的详细内容,更多关于python 直方图均衡化的资料请关注脚本之家其它相关文章!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值