![](/img/trans.png)
[英]How to go from Python numpy 3D array to 2D to 1D back to 2D (preserving the original 2nd and 3rd dimension of the 3D array)
[英]Pythonic way to assign 3rd Dimension of Numpy array to 1D Array
我試圖將已轉換為3D numpy數組的圖像展平為三個單獨的1D數組,分別表示RGB通道。
圖像數組的形狀是(HEIGHT,WIDTH,RGB),並且我嘗試使用索引切片和解壓縮來僅返回第3維值是徒勞的。
理想情況下,三個獨立的陣列代表每個RGB通道,
例:
print(image)
[
[ [56, 6, 3], [23, 32, 53], [27, 33, 56] ],
[ [57, 2, 3], [23, 246, 49], [29, 253, 58] ]
]
red_channel, green_channel, blue_channel = get_third(image)
print(red_channel)
[56, 23, 27, 57, 23, 29]
我曾經考慮過使用嵌套的for循環在前兩個維度上進行迭代,然后將每個RGB數組添加到列表中,或者不添加到列表中,但這是我的理解,這既效率低下又讓人有些費解。
提前致謝!
編輯
說明:通過解壓縮,我的意思是在zip函數中使用star運算符(*),如下所示:
zip(*image)
同樣要澄清的是,我無意保留寬度和高度,我只想基本上只展平並返回數組的3D尺寸。
red_channel, green_channel, blue_channel = np.transpose(np.reshape(image, (-1, 3)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.