[英]How to optimize numpy operation, applying 2D condition on 3 channel RGB image?
I am trying to apply a computation from 2D alpha image to 3 channeled RGB image. 我正在尝试将2D alpha图像的计算应用于3通道RGB图像。 I need to update pixel intensity in each channel, based on respective pixel value in 2D alpha image.
我需要基于2D alpha图像中的相应像素值来更新每个通道中的像素强度。 Below is one MWE I created to illustrate the concept.
以下是我创建的一个MWE来说明这一概念。
MWE: MWE:
# test alpha 2D image
test_a1 = np.array([
[0, 0, 50],
[0, 0, 150],
[0, 0, 225]
])
# test 3 channel RGB image
test_ir1 = np.ones((3,3,3))
# getting indices of alpha where cond is satisfied
idx = np.unravel_index(np.where(test_a1.ravel()>0),test_a1.shape)
test_output = np.zeros_like(test_ir1)
n_idx = len(idx[0][0])
# applying computation on 3 channel RGB image only where cond is satisfied.
for i in range(n_idx):
# multiply only where test_a1 > 0
r_idx, c_idx = idx[0][0][i], idx[1][0][i]
test_output[r_idx,c_idx,0] = test_a1[r_idx, c_idx] * test_ir1[r_idx, c_idx, 0]
test_output[r_idx,c_idx,1] = test_a1[r_idx, c_idx] * test_ir1[r_idx, c_idx, 1]
test_output[r_idx,c_idx,2] = test_a1[r_idx, c_idx] * test_ir1[r_idx, c_idx, 2]
test_output = test_output.astype('uint8')
plt.imshow(test_output, vmin=0, vmax=3)
I basically tried to find the indices in 2D alpha image, where condition is met, and tried to apply those indices to all channels of the image. 我基本上试图在满足条件的2D alpha图像中找到索引,然后尝试将这些索引应用于图像的所有通道。
Is there a way to optimize above operation (not for channel looping)? 有没有一种方法可以优化上述操作(不适用于通道循环)? I am specifically looking to avoid the for loop in the code, doing numpy for each index.
我特别希望避免代码中的for循环,对每个索引执行numpy。 It is ver slow for regular images.
对于常规图像来说速度很慢。
You might observe that: 您可能会观察到:
test_output = np.einsum('ij,ijk->ijk', test_a1, test_ir1)
Not sure if this helps reformulate a slightly different MWE if this isnt precisely what you intend 如果这不是您想要的目的,则不确定这是否有助于重新制定稍有不同的MWE
=== edited === ===编辑===
I would still make use of einsum
since it allows you a lot of control of vectorised multidimensional linear algebra. 我仍然会使用
einsum
因为它允许您对矢量化的多维线性代数进行大量控制。
Provided you can reduce your operations to some mathematical description, for example: 只要您可以将操作简化为一些数学描述,例如:
Where
test_a1
is greater than zero, double the intensity of the pixels measured over each channel.如果
test_a1
大于零,则在每个通道上测得的像素强度加倍。
Then you do that in the following way: 然后,您可以通过以下方式进行操作:
mask = test_a1 > 0
output = np.einsum('ij,ijk->ijk', mask, test_ir1) + test_ir1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.