【问题标题】:Array Indexing in multi dimensional numpy array多维numpy数组中的数组索引
【发布时间】:2013-06-05 01:00:39
【问题描述】:

我是 numpy 的新手,并试图从 here 中理解以下示例。我无法理解

的输出
>>> 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]]])

【问题讨论】:

    标签: python multidimensional-array numpy indexing


    【解决方案1】:

    您正在创建一个 3D 数组,其中第一个 2D 数组(带有 3D 数组)是通过从 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
    

    【讨论】:

      【解决方案2】:

      这可能有助于您理解:

      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]
      

      【讨论】:

        猜你喜欢
        • 2015-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-22
        • 2020-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多