我想將所有像素值轉換為 0 而不是 255 的值。像素值保存在 Numpy 陣列中,即 x 和:
x.shape = (100, 1, 256, 256)
如何使用條件操作陣列?
我嘗試了以下但發生錯誤“ValueError:具有多個元素的陣列的真值不明確。使用 a.any() 或 a.all()”
i=0
for i in x[i]:
if x[i]==255:
x[i] = x[i]
else:
x[i] ==0
uj5u.com熱心網友回復:
只需使用:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/468342.html
下一篇:將卷積層的輸出更改為張量元組
