Numpy中的像素操作



我想将所有像素值转换为0而不是255的值。像素值保存在Numpy数组中,即x和:

x.shape = (100, 1, 256, 256)

如何使用条件操作数组?

我尝试了以下操作,但是出现了错误"ValueError:包含多个元素的数组的真值是不明确的。使用a.a any()或a.a all()">

i=0
for i in x[i]:
if x[i]==255:
x[i] = x[i]
else:
x[i] ==0

直接使用:

x[x==255] = 0

测试:

# Repeatable randomness
np.random.seed(42)
# Synthesise array
x = np.random.randint(0,256, (100, 1, 256, 256), np.uint8)
# Count number of 255s
len(np.where(x==255)[0])    # result = 25671
# Make each 255 into 0
x[x==255] = 0
# Count number of 255s
len(np.where(x==255)[0])    # result = 0

最新更新