简体   繁体   English

如何用另一个数组的相同索引中的值替换一个数组中的值?

[英]How can I replace a value from one array with a value in the same index of another array?

I have two 3D numpy arrays which represent two images. 我有两个表示两个图像的3D numpy数组。 The shape of each array is (1080, 1920, 3). 每个数组的形状是(1080,1920,3)。 The number 3 represents the RGB value of each pixel in the image. 数字3表示图像中每个像素的RGB值。

My goal is to replace every non-black pixel in the first array to the value of the "parallel" pixel (in the same index) from the other array. 我的目标是将第一个数组中的每个非黑色像素替换为另一个数组中“平行”像素(在相同索引中)的值。

How can I do this using only numpy methods? 如何仅使用numpy方法来做到这一点?

Use a mask with True/False values 使用具有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.

相关问题 如何用另一个数组中唯一值的索引替换numpy数组中的重复值? - How can I replace recurring values in a numpy array by the index of the unique value from another array? 用另一个数组的相同索引的值替换一个数组中的值? - Replacing a value from one array with a value of the same index of another array? 如何将值从一个数组推送到另一个数组,一个接一个的索引 - How to push value from an array to another, one by one index 使用numpy,如何生成一个数组,其中每个索引的值是从0到第二个数组中相同索引的值的总和? - Using numpy, how can I generate an array where the value at each index is the sum of the values from 0 to that same index in a second array? 如何通过 python 在另一个数组中找到具有相同值的索引? - How to find index that has same value in another array by python? 如何使用索引定义数组中的值作为索引来定义数组中的值 - How can I use values from an index-defining array as the index to define a value in an array 使用同一数组中的另一个值作为索引替换numpy数组中的值 - Replacing value in a numpy array using as an index another value from the same array 如何从这个 JSON ARRAY 中提取一个值? - How can I extract one value from this JSON ARRAY? 根据另一个数组的索引和另一个数组的值求一个数组的最小值 - Finding the minimum value of an array subject to an index of another array and values from another one 我需要在一个numpy数组中找到值的位置,并使用它来引用另一个numpy数组的相同位置中的值 - I need to find the location of a value in one numpy array and use it to refer to a value in the same location of another numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM