[英]Subtract 2D array from each pixel of a 3D image and get a 4D array
我有一個形狀為(10, 3)
的2D
數組和一個表示為形狀為(480, 640, 3)
的3D
數組的圖像。 我想在2D
數組的每個像素和每個元素之間執行差異,以獲得形狀(10, 480, 640, 3)
的最終結果。 目前,我的代碼如下所示:
arr_2d = np.random.rand(10, 3)
arr_3d = np.random.rand(480, 640, 3)
res = np.ones_like(arr_3d)
res = np.tile(res, (10, 1, 1, 1))
for i in range(10):
res[i] = arr_3d - arr_2d[i]
我的問題是,如果沒有 for 循環,是否有辦法做到這一點,只使用 numpy 操作。
您可以嘗試像這樣使用 np.array 進行廣播
arr_2d = arr_2d.reshape(-1,1,1,3)
arr_3d = arr_3d.reshape((-1,*arr_3d.shape))
res = arr_3d - arr_2d
這應該給出與原始代碼相同的結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.