• Matplotlib入门[04]——处理图像


    Matplotlib入门[04]——处理图像

    参考:

    图片来源:百度(如有侵权,立删)

    使用Jupyter进行练习

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Hh5lvXwt-1670315527470)(https://matplotlib.org/stable/_static/images/logo2.svg)]

    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    import numpy as np
    
    • 1
    • 2
    • 3

    Cricket.png

    img

    导入图像

    首先导入上面的图像,注意 matplotlib 默认只支持 PNG 格式的图像,可以使用 mpimg.imread 方法读入这幅图像:

    img = mpimg.imread('Cricket.png')
    print("shape: ",img.shape)
    print("dtype: ",img.dtype)
    
    • 1
    • 2
    • 3
    shape:  (463, 719, 3)
    dtype:  float32
    
    • 1
    • 2

    这是一个 463 x 719 x 3RGB 图像,并且每个像素使用 uint8 分别表示 RGB 三个通道的值。不过在处理的时候,matplotlib 将它们的值归一化0.0~1.0 之间:

    显示图像

    使用 plt.imshow() 可以显示图像:

    imgplot = plt.imshow(img)
    
    • 1

    在这里插入图片描述

    伪彩色图像

    从单通道模拟彩色图像:

    lum_img = img[:,:,0]
    imgplot = plt.imshow(lum_img)
    
    • 1
    • 2

    在这里插入图片描述

    改变colormap

    imgplot = plt.imshow(lum_img)
    imgplot.set_cmap('hot')
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    显示色度条

    imgplot = plt.imshow(lum_img)
    imgplot.set_cmap('GnBu')
    plt.colorbar()
    plt.show()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    限制显示范围

    查看直方图

    plt.hist(lum_img.flatten(), 256, range=(0.0,1.0), fc='k', ec='k')
    plt.show()
    
    • 1
    • 2

    在这里插入图片描述

    将显示范围设为 0.0-0.8:

    imgplot = plt.imshow(lum_img)
    imgplot.set_clim(0.0,0.8)
    
    • 1
    • 2

    在这里插入图片描述

  • 相关阅读:
    Thrift安装配置
    01_Linux字符设备驱动开发
    这 30 个常用的 Maven 命令你必须熟悉!
    OGG将Oracle全量同步到kafka
    lru_cache python
    D咖饮品机器人惊艳亮相:智能硬件改变生活习惯
    线程(售票小程序)
    Linux下线程编程
    8个独立键盘驱动程
    Maven 01
  • 原文地址:https://blog.csdn.net/weixin_47692652/article/details/128205454