gpt4 book ai didi

python - 使用 numpy 用图案掩盖图像?

转载 作者:IT老高 更新时间:2023-10-28 20:53:50 28 4
gpt4 key购买 nike

我正在使用 numpy 来构建像素数组。一个 800x600 的图像是一个 uint8、800x600x3 的 3 维数组。我也有一个具有固定模式的类似数组(棋盘格,参见 here)。我有另一个数组,800x600 的掩码值。在掩码为零的情况下,我想将图案像素复制到图像像素。在掩码不为零的情况下,我想不理会图像像素。

>>> image.shape
(800, 600, 3)
>>> chex.shape
(800, 600, 3)
>>> mask.shape
(800, 600)

感觉应该可以了:

image[mask == 0,...] = chex

但给出“ValueError: array is not broadcastable to correct shape”。

如何将 chex 像素复制到掩码为零的图像像素?

最佳答案

idx=(mask==0)
image[idx]=chex[idx]

请注意,image 的形状为 (800,600,3),而 idx 的形状为 (800,600)。 The rules用于索引状态

if the selection tuple is smaller than n, then as many : objects as needed are added to the end of the selection tuple so that the modified selection tuple has length N.

因此,索引数组具有自己的广播能力。 idx 的形状被提升为 (800,600,:)

关于python - 使用 numpy 用图案掩盖图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2169605/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com