python - 如何用另一个数组的同一索引中的值替换一个数组中的值?
问题描述
我有两个代表两个图像的 3D numpy 数组。每个数组的形状是 (1080, 1920, 3)。数字 3 代表图像中每个像素的 RGB 值。
我的目标是将第一个数组中的每个非黑色像素替换为另一个数组中“平行”像素(在同一索引中)的值。
我怎样才能只使用 numpy 方法来做到这一点?
解决方案
使用具有真/假值的掩码
# 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])
推荐阅读
- excel - 排序日期并显示最近的 VBA
- node.js - 在多个项目之间共享和管理 rc 文件(或配置文件)
- ios - API 调用期间 Swift valueNotFound 的问题
- javascript - 如何从数组中获取第一个索引,这是javascript中对象的第一个值?
- javascript - Safari 在移动设备中忽略 img 中的自动高度
- java - ArrayList 从类对象中更简单的方法是什么?
- android - 在 Room 中使用成员变量进行查询
- java - 从 json 中读取日期为数组整数,并将 POJO 映射为整数
- swiftui - 如何在 swiftui 中创建键盘应用程序扩展?
- python-3.x - 在 Ubuntu 中读取 Python Logging 日志文件