zoukankan      html  css  js  c++  java
  • python库skimage 绘制直方图;绘制累计直方图;实现直方图匹配(histogram matching)

    绘制直方图

    from skimage import exposure
    # 绘制彩色图像的c通道的直方图
    img_hist, bins = exposure.histogram(img[..., c], source_range='dtype')
    # 以第c行第i列的形式绘制归一化直方图
    axes[c, i].plot(bins, img_hist / img_hist.max())
    

    绘制累积直方图

    from skimage import exposure
    img_cdf, bins = exposure.cumulative_distribution(img[..., c])
    axes[c, i].plot(bins, img_cdf)
    

    直方图匹配(histogram matching)

    含义:使源图像的累积直方图和目标图像一致

    from skimage.exposure import match_histograms
    # 参数1:源图像;参数2:目标图像;参数3:多通道匹配
    matched = match_histograms(image, reference, multichannel=True)
    

    实验:直方图匹配效果

    """
    ==================
    Histogram matching
    ==================
    
    This example demonstrates the feature of histogram matching. It manipulates the
    pixels of an input image so that its histogram matches the histogram of the
    reference image. If the images have multiple channels, the matching is done
    independently for each channel, as long as the number of channels is equal in
    the input image and the reference.2
    
    Histogram matching can be used as a lightweight normalisation for image
    processing, such as feature matching, especially in circumstances where the
    images have been taken from different sources or in different conditions (i.e.
    lighting).
    """
    
    import matplotlib.pyplot as plt
    
    from skimage import data
    from skimage import exposure
    from skimage.exposure import match_histograms
    
    reference = data.coffee()
    image = data.chelsea()
    
    matched = match_histograms(image, reference, multichannel=True)
    
    fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3),
                                        sharex=True, sharey=True)
    for aa in (ax1, ax2, ax3):
        aa.set_axis_off()
    
    ax1.imshow(image)
    ax1.set_title('Source')
    ax2.imshow(reference)
    ax2.set_title('Reference')
    ax3.imshow(matched)
    ax3.set_title('Matched')
    
    plt.tight_layout()
    plt.show()
    
    
    ######################################################################
    # To illustrate the effect of the histogram matching, we plot for each
    # RGB channel, the histogram and the cumulative histogram. Clearly,
    # the matched image has the same cumulative histogram as the reference
    # image for each channel.
    
    fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(8, 8))
    
    
    for i, img in enumerate((image, reference, matched)):
        for c, c_color in enumerate(('red', 'green', 'blue')):
            img_hist, bins = exposure.histogram(img[..., c], source_range='dtype')
            axes[c, i].plot(bins, img_hist / img_hist.max())
            img_cdf, bins = exposure.cumulative_distribution(img[..., c])
            axes[c, i].plot(bins, img_cdf)
            axes[c, 0].set_ylabel(c_color)
    
    axes[0, 0].set_title('Source')
    axes[0, 1].set_title('Reference')
    axes[0, 2].set_title('Matched')
    
    plt.tight_layout()
    plt.show()
    

    实验输出

    左图:源图像;中图:目标图像(参考图像);右图:源图直方图匹配后图像
    直方图匹配操作含义展示:可以看到匹配后,源图像和目标图像的累积直方图趋于一致

  • 相关阅读:
    svn自动部署
    【阿里云】Windows2008 FTP配置指南
    thinkphp 3.2多语言设置
    iphone、ipod Touch、ipad触屏时的js事件
    Nginx区分PC或手机访问不同网站
    Nginx Location配置总结及基础最佳实践
    阿里云官方教程 Linux 系统挂载数据盘
    利用php调用so库文件中的代码
    LINUX下PHP编译添加相应的动态扩展模块so(不需要重新编译PHP,以openssl.so为例)
    Android深入理解Context(三)Context完全解析
  • 原文地址:https://www.cnblogs.com/wojianxin/p/12641405.html
Copyright © 2011-2022 走看看