unicorn_poet
unicorn_poet

Reputation: 495

Zero out portion of multidim numpy array

I have an numpy array with dimensions (200, 200, 3). It is an RGB image.

I also have the (xmin,ymin,xmax,ymax) coordinates of a region of this image that I would like to set to zero. This region should be zero in all three channels.

I can of course solve this with a loop, but that would be wasteful.

Is there a simple way to mask the array using numpy?

Upvotes: 4

Views: 1519

Answers (1)

tmdavison
tmdavison

Reputation: 69116

Use array slicing. If xmin, xmax, ymin and ymax are the indices of area of the array you want to set to zero, then:

a[xmin:xmax,ymin:ymax,:] = 0.

Upvotes: 2

Related Questions