NumPy Broadcast:在 (M,A,B,N) 形数组中的元素 (a,b) 添加一个 ndarray
NumPy Broadcast: Adding an ndarray across elements (a,b) in (M,A,B,N) shaped array
我有一个 rgb 像素的图像数组,所以该数组的尺寸为 (image_count,height,width,3)
我正在按照我在侧面计算的随机量调整单个像素,形状为:(image_count,3)
。
现在,我有一个解释循环来将调整添加到各个像素。
我如何广播这个加法,以便对每个像素执行以下操作:
image_set[i,h,w,:] += pixel_adj[i,:]
用None/np.newaxis
at appropriate dimensions for pixel_adj
to make it 4D
, just like image_set
and then perform the summation adjustment to bring in broadcasting
引入新轴,像这样-
image_set += pixel_adj[:,None,None,:]
我有一个 rgb 像素的图像数组,所以该数组的尺寸为 (image_count,height,width,3)
我正在按照我在侧面计算的随机量调整单个像素,形状为:(image_count,3)
。
现在,我有一个解释循环来将调整添加到各个像素。
我如何广播这个加法,以便对每个像素执行以下操作:
image_set[i,h,w,:] += pixel_adj[i,:]
用None/np.newaxis
at appropriate dimensions for pixel_adj
to make it 4D
, just like image_set
and then perform the summation adjustment to bring in broadcasting
引入新轴,像这样-
image_set += pixel_adj[:,None,None,:]