从具有 3 个通道的 numpy 数组绘制彩色图像

Abh*_*hik 8 python numpy matplotlib

在我的 Jupyter 笔记本中,我试图显示我正在通过 Keras 迭代的图像。我使用的代码如下

def plotImages(path, num):
 batchGenerator = file_utils.fileBatchGenerator(path+"train/", num)
 imgs,labels = next(batchGenerator)
 fig = plt.figure(figsize=(224, 224))
 plt.gray()
 for i in range(num):
    sub = fig.add_subplot(num, 1, i + 1)
    sub.imshow(imgs[i,0], interpolation='nearest')
Run Code Online (Sandbox Code Playgroud)

但这只是绘制单通道,所以我的图像是灰度的。如何使用 3 个通道输出彩色图像图。?

Sue*_*ver 10

如果要显示 RGB 图像,则必须提供所有三个通道。根据您的代码,您只显示第一个通道,因此matplotlib没有将其显示为 RGB 的信息。相反,它会将值gray映射到颜色图,因为您已经调用了plt.gray()

相反,您需要将 RGB 图像的所有通道传递给imshow,然后使用真彩色显示并忽略图形的颜色图

sub.imshow(imgs, interpolation='nearest')
Run Code Online (Sandbox Code Playgroud)

更新

由于imgs实际上是2 x 3 x 224 x 224,您需要在显示图像之前索引imgs并排列尺寸224 x 224 x 3

im2display = imgs[1].transpose((1,2,0))
sub.imshow(im2display, interpolation='nearest')
Run Code Online (Sandbox Code Playgroud)