• 挑战图像处理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))
    
    在这里插入图片描述 在这里插入图片描述
  • 相关阅读:
    shell编程-基础
    磁盘管理-下部
    磁盘管理-中部
    磁盘管理-上部
    用户的管理
    docker之阿里云centos 7.x 启动容器报错处理办法
    IDEA之整合SVN遇到的坑(一)
    springboot之通过idea打jar包并运行
    SpringBoot整合定时任务和异步任务处理
    Microsoft SQL Server 2012安装说明
  • 原文地址:https://www.cnblogs.com/Jack-Tim-TYJ/p/12831902.html
Copyright © 2020-2023  润新知