我有帶有 rgb 像素資料的二維陣列(2 行,一行 3 個像素)。
[[[255, 255, 255],[3, 0, 2],[255, 255, 255]],[[255, 255, 255],[3, 0, 2],[255, 255, 255]]]
我怎樣才能獲得獨特的像素?我想得到
[[255, 255, 255], [3, 0, 2]]
我正在嘗試使用np.uniqueand np.transposewithnp.reshape但我無法獲得預期的結果。
uj5u.com熱心網友回復:
將陣列重塑為 2D,然后使用np.uniquewithaxis=0
arr = np.array([[[255, 255, 255],[3, 0, 2],[255, 255, 255]],[[255, 255, 255],[3, 0, 2],[255, 255, 255]]])
shape = arr.shape
arr = arr.reshape((shape[0] * shape[1], shape[2]))
print(np.unique(arr, axis=0))
輸出
[[ 3 0 2]
[255 255 255]]
uj5u.com熱心網友回復:
這個怎么樣?
import itertools
np.unique(np.array(list(itertools.chain(*arr))), axis=0)
array([[ 3, 0, 2],
[255, 255, 255]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/518089.html
標籤:Python麻木的
