gpt4 book ai didi

python - 从 3D Numpy 数组绘制红色 channel

转载 作者:太空狗 更新时间:2023-10-29 21:39:29 28 4
gpt4 key购买 nike

假设我们有一个 RGB 图像,我们已使用以下代码将其转换为 Numpy 数组:

import numpy as np
from PIL import Image

img = Image.open('Peppers.tif')
arr = np.array(img) # 256x256x3 array

如果我们只对可视化红色 channel 感兴趣,即 arr[:,:,0],我们如何绘制这个 2D Numpy 数组?

最佳答案

您可以使用 matplotlib 的 imshow():

import matplotlib.pyplot as plt
imgplot = plt.imshow(arr[:, :, 0])

查看更多示例 here , 用于插值、颜色条等。

例如,要更改颜色图,您可以执行 imgplot.set_cmap('hot')。将插值设置为 'nearest' 也很有用,如果您真的不想要插值:查看差异

t = np.array([[0, 1, 2], [1, 2, 3], [3, 2, 1]])
import matplotlib.pyplot as plt
plt.imshow(t)
plt.show()
plt.imshow(t, interpolation='nearest')
plt.show()

结果 enter image description here

enter image description here

关于python - 从 3D Numpy 数组绘制红色 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32340306/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com