![](/img/trans.png)
[英]How can I replace recurring values in a numpy array by the index of the unique value from another array?
[英]How can I replace a value from one array with a value in the same index of another array?
我有两个表示两个图像的3D numpy数组。 每个数组的形状是(1080,1920,3)。 数字3表示图像中每个像素的RGB值。
我的目标是将第一个数组中的每个非黑色像素替换为另一个数组中“平行”像素(在相同索引中)的值。
如何仅使用numpy方法来做到这一点?
使用具有True / False值的蒙版
# All pixels should be normalized 0..1 or 0..254
first_img = np.random.rand(1920,1080,3)
second_img = np.random.rand(1920,1080,3)
eps = 0.01 # Black pixel threshold
mask = first_img.sum(axis=2) > eps
for i in range(first_img.shape[2]):
first_img[:,:,i] = (first_img[:, :, i] * mask) + ((1 - mask) * second_img[:, :, i])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.