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

(编辑:jimmy 日期: 2024/9/22 浏览:2)

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:原图的灰度图

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

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

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

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

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

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

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

图5:灰度变换函数

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

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

一句话新闻

Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。