zoukankan      html  css  js  c++  java
  • 挑战图像处理100问(22)——直方图平坦化

    在这里插入图片描述
    最近这几问。。。无脑操作。

    直方图平坦化

    让直方图的平均值m0=128m_0=128,标准差s0=52s_0=52​吧!

    这里并不是变更直方图的动态范围,而是让直方图变得平坦。

    可以使用下式将平均值为mm标准差为ss的直方图变成平均值为m0m_0标准差为s0s_0的直方图:
    xout=s0s (xinm)+m0 x_{out}=frac{s_0}{s} (x_{in}-m)+m_0

    代码实现

    import numpy as np
    import matplotlib.pyplot as plt
    from skimage.io import imread # 用来读取图片
    %matplotlib inline
    
    # 读取图片
    path = 'C:/Users/86187/Desktop/image/'
    file_in = path + 'cake.jpg' 
    img = imread(file_in)
    
    plt.figure
    imgshow = plt.imshow(img)
    

    在这里插入图片描述

    # 灰度化
    # 灰度化函数
    def BGR2GRAY(img):
    
        # 获取图片尺寸
        H, W, C = img.shape
    
        # 灰度化
        out = np.ones((H,W,3))
        for i in range(H):
            for j in range(W):
                out[i,j,:] = 0.299*img[i,j,0] + 0.578*img[i,j,1] + 0.114*img[i,j,2]
    
        out = out.astype(np.uint8)
    
        return out
    
    img = BGR2GRAY(img)
    plt.figure
    imgshow = plt.imshow(img)
    

    在这里插入图片描述

    img_ = img.copy()
    img_ = img_.reshape(-1)
    hist = plt.hist(img_, bins=255, rwidth=0.85, range=(0,255))
    

    在这里插入图片描述

    def evenHist(img,m0,s0):
        m = img.mean()
        s = np.sqrt(img.var())
        img = (s0/s)*(img-m)+m0
        img = img.astype(np.uint8)
        return img
    
    img2 = img.copy()
    img2 = evenHist(img2,128,52)
    imgshow = plt.imshow(img2)
    plt.show()
    hist1 = plt.hist(img2.reshape(-1),bins=255,rwidth=0.85,range=(0,255))
    
    在这里插入图片描述 在这里插入图片描述
  • 相关阅读:
    关于git 拉取的时候一直弹输入密码的问题
    开始日期结束日期check问题
    关于boostrap 排版问题
    【DP_树形DP专题】题单总结
    【DP_背包专题】 背包九讲
    Ubuntu不卸载ibus前提下安装搜狗输入法
    Ubuntu下Java环境配置
    Ubuntu下gcc及g++环境配置
    Ubuntu下VIM(GVIM)环境配置
    PAT 1065 A+B and C (64bit) (20)
  • 原文地址:https://www.cnblogs.com/Jack-Tim-TYJ/p/12831902.html
Copyright © 2011-2022 走看看