简体   繁体   English

使用2D遮罩将3D numpy数组写入较大的3D数组的切片中

[英]Writing a 3D numpy array into a slice of a larger 3D array using a 2D mask

I have two 3D numpy arrays, each representing an image (x, y, rgb). 我有两个3D numpy数组,每个数组代表一个图像(x,y,rgb)。 I want to write the smaller image into the larger image, except for any magenta pixels (255, 0, 255). 我想将较小的图像写入较大的图像,但洋红色像素(255、0、255)除外。 I know how to generate a 2D mask representing all the magenta pixels -- how can I use this 2D mask in a 3D array operation? 我知道如何生成代表所有洋红色像素的2D蒙版-如何在3D阵列操作中使用此2D蒙版?

Here is one example. 这是一个例子。 It's not precisely accurate (all values are one or two), but gives the idea. 它不是精确准确的(所有值都是一或两个),但是给出了主意。 Hopefully this works in your situation: 希望这在您的情况下有效:

image1 = np.ones((5,5,16))
image2 = 2 * np.ones((3,3,16))
mask = np.array([[0, 1, 1], [0, 0, 1], [1, 0, 0]])
x, y = np.where(mask)
image1[1:4,1:4,:][x,y,:] = image2[x,y,:]

( 1:4,1:4 is the position of the subimage inside the larger image.) 1:4,1:4是子图像在较大图像内的位置。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM