【问题标题】:How to correctly display red, green, and blue (rgb) channels of an image with pyplot如何使用 pyplot 正确显示图像的红色、绿色和蓝色 (rgb) 通道
【发布时间】:2017-11-28 21:28:34
【问题描述】:

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

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

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

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()

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

【问题讨论】:

  • 嗨@Abraham Gutierrez,欢迎来到SO。我了解您没有足够的声誉在帖子中发布图片,但是,在未来,请这样做。如果人们不必去外部页面,他们更有可能查看您的问题。 img1.shapetemp.min()temp.max() 是什么?另外,您能否发布原始图像以便我们重现您的错误?
  • 对不起。我使用了图像按钮,但编辑器将它们作为链接。 img1.shape 是加载图像的尺寸,据我所知(x,y,rgb 通道)。 min() 和 max() 函数分别返回任何数组/矩阵的最小值和最大值。

标签: python image numpy matplotlib rgb


【解决方案1】:

你只需要一点点改动: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()

【讨论】:

  • 非常感谢。我错过了你添加的行。此外,img1.shape 返回 (x,y,4)(可能适用于 RGBA 格式)而不是 (x,y,3)(仅适用于 RGB)。
猜你喜欢
  • 2022-12-06
  • 2016-09-22
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多