直方图均衡

# 作者:喵酱
# 开发时间:2022/4/16 13:32
"""导入需要用的python库"""
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image
plt.rcParams['font.sans-serif'] = ['FangSong']  # 设置字体以便正确显示中文


def get_pdf(img):
    """获取图像的概率密度"""
    N = img.shape[0] * img.shape[1]               # 计算图像总像素个数
    return [np.sum(img == i)/N for i in range(256)]  # 计算概率密度


def hist_equal(in_img):
    """直方图均衡"""
    pdf = get_pdf(in_img)       # 获取图像的概率密度
    out_img = np.copy(in_img)   # 构造输出图像
    sum_n = 0                   # 存储相同像素值的像素个数
    for i in range(256):
        sum_n = sum_n + pdf[i]
        out_img[(in_img == i)] = sum_n*255
    return out_img


img = np.asarray(Image.open('la.tif'))      # 读入原图
img2 = hist_equal(img)                  # 对原图直方图均衡化
# 显示图像
plt.subplot(221)
plt.title('原始图像')
plt.imshow(img, cmap='gray')

plt.subplot(222)
plt.title('原图灰度直方图')
plt.hist(img)           # 绘制直方图

plt.subplot(223)
plt.title('直方图均衡化图像')
plt.imshow(img2, cmap='gray')

plt.subplot(224)
plt.title('直方图均衡化后图像的灰度直方图')
plt.hist(img2)          # 绘制直方图

plt.show()




 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值