Tom Quinn
Tom Quinn

Reputation: 623

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). I want to write the smaller image into the larger image, except for any magenta pixels (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?

Upvotes: 0

Views: 103

Answers (1)

user707650
user707650

Reputation:

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.)

Upvotes: 2

Related Questions