keras - VOC2012 : PIL Image. open 将PNG转换为二维数组

标签 keras deep-learning python-imaging-library python-imageio

我正在使用 VOC2012 数据集。当我使用 imageio 打开图像时,输入图像为 PNG 格式,其形状为 (375, 500, 4)。当我使用 PIL 打开图像时,形状突然变成 (500, 375)。 PNG 图像在最后一个轴上应该有四个维度:r g b 和 alpha。 Here is a sample image

图像显然是彩色图像,所以它应该有 3 个维度(高度、宽度、深度)。 PIL 似乎暗示它只有两个维度:宽度和高度。

PNG图像可以用二维数组表示吗?请帮忙!所以一时迷失了方向。谢谢!

from PIL import Image
from keras.preprocessing.image import img_to_array
import os, imageio
import numpy as np

root_path = '/Users/johnson/Downloads/'

imageio_img = imageio.imread(
    os.path.join(root_path, '2009_003193.png')
)

# (375, 500, 4)
print(imageio_img.shape)
# [  0 128 192 224 255]
print(np.unique(imageio_img))


PIL_img = Image.open(
    os.path.join(root_path, '2009_003193.png')
)

# (500, 375)
print(PIL_img.size)

PIL_img_to_array = img_to_array(PIL_img)
# (375, 500, 1)
print(PIL_img_to_array.shape)
# [  0.   2. 255.]
print(np.unique(PIL_img_to_array))


PIL 似乎知道 VOC2012 是如何标记数据的,这也是相当神奇的。 PIL_image_to_array具有唯一值 [0, 2, 255] .方便的是,2 表示 VOC2012 中的自行车。 0 表示背景,255 可能表示自行车周围的黄色边界。但是从第一个代码片段开始,我从未将 pascal 类传递给 PIL 进行转换。
def pascal_classes():
  classes = {'aeroplane' : 1,  'bicycle'   : 2,  'bird'        : 3,  'boat'         : 4,
             'bottle'    : 5,  'bus'       : 6,  'car'         : 7,  'cat'          : 8,
             'chair'     : 9,  'cow'       : 10, 'diningtable' : 11, 'dog'          : 12,
             'horse'     : 13, 'motorbike' : 14, 'person'      : 15, 'potted-plant' : 16,
             'sheep'     : 17, 'sofa'      : 18, 'train'       : 19, 'tv/monitor'   : 20}

  return classes

def pascal_palette():
  palette = {(  0,   0,   0) : 0 ,
             (128,   0,   0) : 1 ,
             (  0, 128,   0) : 2 ,
             (128, 128,   0) : 3 ,
             (  0,   0, 128) : 4 ,
             (128,   0, 128) : 5 ,
             (  0, 128, 128) : 6 ,
             (128, 128, 128) : 7 ,
             ( 64,   0,   0) : 8 ,
             (192,   0,   0) : 9 ,
             ( 64, 128,   0) : 10,
             (192, 128,   0) : 11,
             ( 64,   0, 128) : 12,
             (192,   0, 128) : 13,
             ( 64, 128, 128) : 14,
             (192, 128, 128) : 15,
             (  0,  64,   0) : 16,
             (128,  64,   0) : 17,
             (  0, 192,   0) : 18,
             (128, 192,   0) : 19,
             (  0,  64, 128) : 20 }

最佳答案

您的图像是托盘化的,而不是 RGB。每个像素由调色板中的 8 位索引表示。您可以通过查看 image.mode 来了解这一点。显示为 P .

如果您想要 RGB 图像,请使用:

rgb = Image.open('bike.png').convert('RGB')

如果您想要具有透明度的 RGBA 图像,请使用:
RGBA = Image.open('bike.png').convert('RGBA')

但是,Alpha channel 中没有有用的信息,因此这似乎毫无意义。

关于 pascal 调色板,您可以通过 PIL 获得它,如下所示:
im = Image.open('bike.png')                                                                 

p = im.getpalette()

for i in range (256): 
    print(p[3*i:3*i+3])

[0, 0, 0]
[128, 0, 0]
[0, 128, 0]
[128, 128, 0]
[0, 0, 128]
[128, 0, 128]
[0, 128, 128]
[128, 128, 128]
[64, 0, 0]
[192, 0, 0]
[64, 128, 0]
[192, 128, 0]
[64, 0, 128]
[192, 0, 128]
[64, 128, 128]
[192, 128, 128]
[0, 64, 0]
[128, 64, 0]
[0, 192, 0]
[128, 192, 0]
[0, 64, 128]
[128, 64, 128]
[0, 192, 128]
[128, 192, 128]
[64, 64, 0]
[192, 64, 0]
[64, 192, 0]
[192, 192, 0]
[64, 64, 128]
[192, 64, 128]
[64, 192, 128]
[192, 192, 128]
[0, 0, 64]
[128, 0, 64]
[0, 128, 64]
[128, 128, 64]
[0, 0, 192]
[128, 0, 192]
[0, 128, 192]
[128, 128, 192]
[64, 0, 64]
[192, 0, 64]
[64, 128, 64]
[192, 128, 64]
[64, 0, 192]
[192, 0, 192]
[64, 128, 192]
[192, 128, 192]
[0, 64, 64]
[128, 64, 64]
[0, 192, 64]
[128, 192, 64]
[0, 64, 192]
[128, 64, 192]
[0, 192, 192]
[128, 192, 192]
[64, 64, 64]
[192, 64, 64]
[64, 192, 64]
[192, 192, 64]
[64, 64, 192]
[192, 64, 192]
[64, 192, 192]
[192, 192, 192]
[32, 0, 0]
[160, 0, 0]
[32, 128, 0]
[160, 128, 0]
[32, 0, 128]
[160, 0, 128]
[32, 128, 128]
[160, 128, 128]
[96, 0, 0]
[224, 0, 0]
[96, 128, 0]
[224, 128, 0]
[96, 0, 128]
[224, 0, 128]
[96, 128, 128]
[224, 128, 128]
[32, 64, 0]
[160, 64, 0]
[32, 192, 0]
[160, 192, 0]
[32, 64, 128]
[160, 64, 128]
[32, 192, 128]
[160, 192, 128]
[96, 64, 0]
[224, 64, 0]
[96, 192, 0]
[224, 192, 0]
[96, 64, 128]
[224, 64, 128]
[96, 192, 128]
[224, 192, 128]
[32, 0, 64]
[160, 0, 64]
[32, 128, 64]
[160, 128, 64]
[32, 0, 192]
[160, 0, 192]
[32, 128, 192]
[160, 128, 192]
[96, 0, 64]
[224, 0, 64]
[96, 128, 64]
[224, 128, 64]
[96, 0, 192]
[224, 0, 192]
[96, 128, 192]
[224, 128, 192]
[32, 64, 64]
[160, 64, 64]
[32, 192, 64]
[160, 192, 64]
[32, 64, 192]
[160, 64, 192]
[32, 192, 192]
[160, 192, 192]
[96, 64, 64]
[224, 64, 64]
[96, 192, 64]
[224, 192, 64]
[96, 64, 192]
[224, 64, 192]
[96, 192, 192]
[224, 192, 192]
[0, 32, 0]
[128, 32, 0]
[0, 160, 0]
[128, 160, 0]
[0, 32, 128]
[128, 32, 128]
[0, 160, 128]
[128, 160, 128]
[64, 32, 0]
[192, 32, 0]
[64, 160, 0]
[192, 160, 0]
[64, 32, 128]
[192, 32, 128]
[64, 160, 128]
[192, 160, 128]
[0, 96, 0]
[128, 96, 0]
[0, 224, 0]
[128, 224, 0]
[0, 96, 128]
[128, 96, 128]
[0, 224, 128]
[128, 224, 128]
[64, 96, 0]
[192, 96, 0]
[64, 224, 0]
[192, 224, 0]
[64, 96, 128]
[192, 96, 128]
[64, 224, 128]
[192, 224, 128]
[0, 32, 64]
[128, 32, 64]
[0, 160, 64]
[128, 160, 64]
[0, 32, 192]
[128, 32, 192]
[0, 160, 192]
[128, 160, 192]
[64, 32, 64]
[192, 32, 64]
[64, 160, 64]
[192, 160, 64]
[64, 32, 192]
[192, 32, 192]
[64, 160, 192]
[192, 160, 192]
[0, 96, 64]
[128, 96, 64]
[0, 224, 64]
[128, 224, 64]
[0, 96, 192]
[128, 96, 192]
[0, 224, 192]
[128, 224, 192]
[64, 96, 64]
[192, 96, 64]
[64, 224, 64]
[192, 224, 64]
[64, 96, 192]
[192, 96, 192]
[64, 224, 192]
[192, 224, 192]
[32, 32, 0]
[160, 32, 0]
[32, 160, 0]
[160, 160, 0]
[32, 32, 128]
[160, 32, 128]
[32, 160, 128]
[160, 160, 128]
[96, 32, 0]
[224, 32, 0]
[96, 160, 0]
[224, 160, 0]
[96, 32, 128]
[224, 32, 128]
[96, 160, 128]
[224, 160, 128]
[32, 96, 0]
[160, 96, 0]
[32, 224, 0]
[160, 224, 0]
[32, 96, 128]
[160, 96, 128]
[32, 224, 128]
[160, 224, 128]
[96, 96, 0]
[224, 96, 0]
[96, 224, 0]
[224, 224, 0]
[96, 96, 128]
[224, 96, 128]
[96, 224, 128]
[224, 224, 128]
[32, 32, 64]
[160, 32, 64]
[32, 160, 64]
[160, 160, 64]
[32, 32, 192]
[160, 32, 192]
[32, 160, 192]
[160, 160, 192]
[96, 32, 64]
[224, 32, 64]
[96, 160, 64]
[224, 160, 64]
[96, 32, 192]
[224, 32, 192]
[96, 160, 192]
[224, 160, 192]
[32, 96, 64]
[160, 96, 64]
[32, 224, 64]
[160, 224, 64]
[32, 96, 192]
[160, 96, 192]
[32, 224, 192]
[160, 224, 192]
[96, 96, 64]
[224, 96, 64]
[96, 224, 64]
[224, 224, 64]
[96, 96, 192]
[224, 96, 192]
[96, 224, 192]
[224, 224, 192]

然后,如果你想让自行车变红,你可以这样做:
# Load the image and make Numpy version
im = Image.open('bike.png') 
n = np.array(im)

# Make all pixels belonging to bike (2) into red (palette index 9)
n[n==2] = 9
# Make all pixels not red (9) into grey (palette index 7)
n[n!=9] = 7

# Convert back into PIL palettised image and re-apply original palette
r = Image.fromarray(n,mode='P') 
r.putpalette(im.getpalette()) 
r.save('result.png') 

enter image description here

关键词 :Python、PIL、Pillow、图像处理、调色板、调色板操作、蒙版图像、蒙版、提取调色板、应用调色板。

关于keras - VOC2012 : PIL Image. open 将PNG转换为二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57010079/

相关文章:

python - Tensorboard 找不到 .runfiles 目录错误

python - Keras 中的 "cell class"是什么?

initialization - 如何在 Keras 中使用任意内核初始化卷积层?

python - 使用 PIL 将 JPG 从 Adob​​eRGB 转换为 sRGB?

python - 是否可以使用 PIL 减少图像的深度?

python - 线性链 CRF 分类器使用哪个激活函数?

python - TensorFlow 中 RNN 的两种实现有什么区别?

neural-network - 使用 Coffee 进行分类时如何使用更通用的标签

machine-learning - 深度学习模型中以前从未见过的类(class)

python - 如何在 Pillow Image.quantize 中使用 kmeans 参数?