【发布时间】:2020-11-16 16:18:04
【问题描述】:
我有一个形状为 (800x1280x4) 的 ndArray - 4 个通道的图像数据。在第 4 通道,即 alpha 通道中,一些值为 1(透明),一些值为 255(不透明)。我想用零替换 r,g,b 通道值,其中 alpha 通道值为 1。
为了说明这一点,我采用了如下示例数组并尝试了以下代码:
>>> import numpy as np
>>>
>>> a = np.random.randint(255, size=(3,5,4))
>>> a
array([[[165, 200, 80, 149],
[247, 126, 88, 2],
[ 35, 24, 59, 167],
[105, 69, 98, 78],
[138, 224, 50, 32]],
[[ 90, 53, 113, 39],
[105, 153, 60, 101],
[139, 249, 105, 79],
[171, 127, 81, 240],
[133, 22, 62, 172]],
[[197, 163, 253, 62],
[193, 57, 208, 247],
[241, 80, 100, 249],
[181, 118, 72, 52],
[221, 121, 89, 138]]])
>>> # I want to replace cell values with zero where 4th column value is < 100
>>> b = np.where(a[...,-1]<100,0,a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 6, in where
ValueError: operands could not be broadcast together with shapes (3,5) () (3,5,4)
我得到了 ValueErrors。根据numpy ndArray中的第4个单元格值替换前三个单元格值的方法是什么?
【问题讨论】:
标签: python numpy-ndarray