zoukankan      html  css  js  c++  java
  • 挑战图像处理100问(21)——直方图归一化

    在这里插入图片描述

    直方图归一化( Histogram Normalization )

    有时直方图会偏向一边。

    比如说,数据集中在00处(左侧)的图像全体会偏暗,数据集中在255255处(右侧)的图像会偏亮。

    如果直方图有所偏向,那么其动态范围( dynamic range )就会较低。

    为了使人能更清楚地看见图片,让直方图归一化、平坦化是十分必要的。

    这种归一化直方图的操作被称作灰度变换(Grayscale Transformation)。像素点取值范围从[c,d][c,d]转换到[a,b][a,b]的过程由下式定义。这回我们将灰度扩展到[0,255][0, 255]范围:
    xout={a(ifxin<c)badc (xinc)+a(else ifcxin<d)b(else) x_{out}= egin{cases} a& ( ext{if}quad x_{in}<c)\ frac{b-a}{d-c} (x_{in}-c)+a&( ext{else if}quad cleq x_{in}<d)\ b&( ext{else}) end{cases}

    代码实现

    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 normalHist(img):
        a = 0
        b = 255
        c = img.min()
        d = img.max()
        img = (b-a)/(d-c)*(img-c)+a
        img = img .astype(np.uint8)
        return img
    
    img1 = img.copy()
    img1 = normalHist(img1)
    imgshow = plt.imshow(img1)
    plt.show()
    hist1 = plt.hist(img1.reshape(-1),bins=255,rwidth=0.85,range=(0,255))
    
    在这里插入图片描述 在这里插入图片描述
  • 相关阅读:
    一个通过JSONP跨域调用WCF REST服务的例子(以jQuery为例)
    步步为营UML建模系列七、表图(Data model diagram)
    步步为营UML建模系列六、类图(Class diagram)
    WebEx
    使用Nancy和Simple.Data两个轻量级的框架打造一个分布式开发系统
    细说 ASP.NET控制HTTP缓存
    WCSF vs ASP.NET MVC
    使用KTM(内核事务管理器)进行文件事务处理
    .net 2.0下的OOXML神器:NPOI.OpenXml4Net
    为什么System.Attribute的GetHashCode方法需要如此设计?
  • 原文地址:https://www.cnblogs.com/Jack-Tim-TYJ/p/12831904.html
Copyright © 2011-2022 走看看