多维numpy数组中的数组索引

我是numpy的新手,并试图从这里理解以下示例。我在理解...的输出时遇到了麻烦


>>> palette[image] 

当索引数组a为多维时,单个索引数组指a的第一维。下面的示例通过使用调色板将标签图像转换为彩色图像来显示此行为。


>>> palette = array( [ [0,0,0],                # black

...                    [255,0,0],              # red

...                    [0,255,0],              # green

...                    [0,0,255],              # blue

...                    [255,255,255] ] )       # white

>>> image = array( [ [ 0, 1, 2, 0 ],           # each value corresponds to a color in the palette

...                  [ 0, 3, 4, 0 ]  ] )

>>> palette[image]                            # the (2,4,3) color image

array([[[  0,   0,   0],

        [255,   0,   0],

        [  0, 255,   0],

        [  0,   0,   0]],

       [[  0,   0,   0],

        [  0,   0, 255],

        [255, 255, 255],

        [  0,   0,   0]]])


互换的青春
浏览 160回答 3
3回答

四季花海

您正在创建一个3D数组,其中第一个2D数组(带有3D数组)是通过从palette的索引所提取的行中给出的,image[0]而第二个数组是通过从palette的索引所给的行中提取出来的image[1]。>>> palette = array( [ [0,0,0],                # black...                    [255,0,0],              # red...                    [0,255,0],              # green...                    [0,0,255],              # blue...                    [255,255,255] ] )       # white>>> image = array( [ [ 0, 1, 2, 0 ],           # each value corresponds to a color in the palette...                  [ 0, 3, 4, 0 ]  ] )>>> palette[image]                            # the (2,4,3) color imagearray([[[  0,   0,   0], # row at index 0 of palete        [255,   0,   0], # index 1        [  0, 255,   0], # index 2        [  0,   0,   0]], # index 0       [[  0,   0,   0], # index 0        [  0,   0, 255], # index 3        [255, 255, 255], # index 4        [  0,   0,   0]]]) # index 0

牛魔王的故事

这可以帮助您了解:array([[[  0,   0,   0],   # palette[0]        [255,   0,   0],   # palette[1]        [  0, 255,   0],   # palette[2]        [  0,   0,   0]],  # palette[0]       [[  0,   0,   0],   # palette[0]        [  0,   0, 255],   # palette[3]        [255, 255, 255],   # palette[4]        [  0,   0,   0]]]) # palette[0]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python