繁体   English   中英

使用 np.array 作为索引更新 3D Numpy 数组

[英]update 3D Numpy array with np.array as indices

我有一个形状 (180, 240, 3) 的图像,它将根据包含每个维度位置的 3 个不同的 numpy arrays 进行修改。

所以三个 numpy arrays 看起来像这样:

height_array = [
  90 148 161 165  40  77  73 108 167  59  56  65 116 165  45  50  99 163
 111 142  73  80 113 156 129  41  78  85 104  10  13  36 116 158  18 122
 170  39 155 173 172 102  53  74  81 160 167 166  21 173 148 177   2   0
   9 155 136 173  48  88  46 127 126 161  51  96  30 165 171  46 106  10
  82 118 179  95 173  93  11 116 117  25 106 164 166  87 109 112 114 179
  32  33  47  50  91  95 106 127 145 165 164 166 171  62  64  87 108 159
 130 168 174 177  35  64 102 160  43  55 105 116 166 178 107 168 164  60
 109  49 146  50  53  52  90  85 107 106 140 165 166   1  42  63  73  85
  84  82 109 112 179  48  94 118 149 134 163   7   4   5  78  64 107 111
 124 168  61 113 145  50  88  98 163  19  95 105  59  52 174  39 106 166
 171 116  28  53  72 118 175 171 163 168   2  66  93   3  66  80 144 134
  92 100 106 112 163  18  62  75  72  88  84  83  82  56  48 148 150 173
  92  59  57]
width_array = [
  13 112 212 141  23  92 127  38 143 170  99  28 127 198 168 179  48 140
  76  20 165 177  74  71  95 168 187  44  91  27 165 171  19  44   2  29
 143  80 174 137 230  28 103  94 210 141 145 196  82 141 135 140  23 176
 126 223  20 145 191 162 166  37 142 142 104   6 162 142 146 146  53 180
  55  22   9 162 146  17 114 221  34 173  95 142 139  49  54  77 130 106
  90  96  42 144  12 210  92   8 238 143 101 186 105 219 221 189  87  12
  54 195 141  29   0 176  36 142 127 183  71 172 113 140  29 142  71 171
  15 108  28  84 138 223  17  32  12  75  57  98   4 156 166  28  31 219
 225 153  71 123  96 180 236  35 219  55   3 151 169 153  96 212 117 183
 110 143 138 184 198 136  25  31 101 159  22 190 112 142 146  80 168 140
 145  67  37 173 207  59 141 141  36 139 191  37   5  48  21 168   5 158
   9  44  34  16   2   2 232 102  73   6  20  43 181 143  34  87 146 146
 193 154 143]
channel_array = [0, 0, 0, 0, 2, 1, ..., 1]

height_array 包含将被修改的像素的 y 轴位置,而 width_array 包含将被修改的像素的 x 轴位置。 channel_array 包含将修改的通道 (R/G/B)。 这三个 arrays 的长度都是一样的,都是 219。

当我尝试使用以下代码修改图像时:

image[height_array][width_array][channel_array] = 255

我收到以下错误:

IndexError: index 230 is out of bounds for axis 0 with size 219

我不确定为什么将其输出为错误。 我在哪里错误地索引了数组?

您的height_array的长度为 219,但您正在尝试访问图像数据中的 position 230,因此请检查您的索引(您在图像上放置了多个蒙版)。

len(height_array)
219

arr = np.zeros(shape=(219,100,100))
arr[230,:,:]

IndexError: index 230 is out of bounds for axis 0 with size 219

暂无
暂无

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

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