plt.imshow

plt.imshow

plt.imshow(X, interpolation=None)

  • X:图像数据

    • (M, N):标量数据的图像,灰度图
    • (M, N, 3):RGB图像
    • (M, N, 4):RGBA图像

注意:其中RGB和RGBA图像为float类型[0, 1],或者int类型[0, 255]

显示图像

Display an image, i.e. data on a 2D regular raster.

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(1)
# x = np.random.rand(25, 25)
x = np.random.rand(25, 25, 3)
# x = np.random.rand(25, 25, 4)


# 可以是float类型[0, 1]
print(x.dtype)
# plt.imshow(x)


# 也可以是int类型[0, 255]
max = np.max(x)
x = x*255/max  # expand pixel from [0, 1] to [0, 255], then normalization
# x = np.uint8(x) # 可以转化为uint8类型
x = x.astype(int) # 也可以转化为int32类型,都是为了使float-->int
print(x.dtype)
plt.imshow(x)
'''
输出:
float64
int32
'''

如果最后不显示图像的话,需要再加一句plt.show()

interpolation参数

这里特别讲一下interpolation参数,此参数显示了不同图像之间的插值方式

下面直接给出官方示例:链接

import matplotlib.pyplot as plt
import numpy as np

methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16',
           'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',
           'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos']

# Fixing random state for reproducibility
np.random.seed(19680801)

grid = np.random.rand(4, 4)

fig, axs = plt.subplots(nrows=3, ncols=6, figsize=(9, 6),
                        subplot_kw={'xticks': [], 'yticks': []})

for ax, interp_method in zip(axs.flat, methods):
    ax.imshow(grid, interpolation=interp_method, cmap='viridis')
    ax.set_title(str(interp_method))

plt.tight_layout()
plt.show()
  • 14
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值