如何使用 pyplot 正确显示图像的红、绿、蓝 (rgb) 通道

Abr*_*ham 5 python rgb numpy image matplotlib

我有一个小项目,所以一周前我开始在 Python 上做一些测试。我必须显示 RGB 图像的 3 个通道,但pyplot.imshow()函数显示以下内容:

当前显示

我想显示红色、绿色和蓝色通道,如下所示:

RGB 的红色通道

到目前为止,这是我的代码:

from matplotlib import pyplot as plt
from PIL import Image
import numpy as np

img1 = np.array(Image.open('img1.png'))
figure, plots = plt.subplots(ncols=3, nrows=1)
for i, subplot in zip(range(3), plots):
    temp = np.zeros(img1.shape, dtype='uint8')
    temp = img1[:,:,i]
    subplot.imshow(temp)
    subplot.set_axis_off()
plt.show()
Run Code Online (Sandbox Code Playgroud)

我不在任何笔记本上工作。相反,我在 PyCharm 中工作。我读了这篇文章:24739769。我查了一下,img1.dtype是的uint8,所以我不知道如何展示我想要的东西。

Y. *_*Luo 6

您只需要进行一项微小的更改:temp[:,:,i] = img1[:,:,i]

使用您显示的第一张图片的完整且可验证的示例:

from matplotlib import pyplot as plt
from PIL import Image
import numpy as np

img1 = np.array(Image.open('img1.png'))
figure, plots = plt.subplots(ncols=3, nrows=1)
for i, subplot in zip(range(3), plots):
    temp = np.zeros(img1.shape, dtype='uint8')
    temp[:,:,i] = img1[:,:,i]
    subplot.imshow(temp)
    subplot.set_axis_off()
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述