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

标签 python numpy matplotlib plot python-imaging-library

假设我们有一个 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/

相关文章:

python - numpy python中的"AttributeError: '矩阵' object has no attribute '等历'"

python - 气泡图加权二维散点图数据到网格/网格

javascript - 如何从网页中抓取 `window.initialState`?

python - 当尝试安装模块时,pip 会安装 python3.5,即使已经安装了 3.7

python - 如何替换pandas中的数据框

python - 在时间序列预测中将对象类型转换为日期时间

集合内的 Python 正则表达式多字符匹配

python - 用于浮点集合的 Python 单元测试中的 assertAlmostEqual

matplotlib 获取给定图形包含的所有轴以应用某些设置

python - 自动将 matplotlib basemap 置于数据中心