繁体   English   中英

多维numpy数组中的数组索引

[英]Array Indexing in multi dimensional numpy array

我是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]]])

要创建一个三维阵列,其中第一2D阵列(withing 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 image
array([[[  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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM