Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 452
unicorn_poet Avatar asked Sep 05 '25 03:09

unicorn_poet


1 Answers

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.
like image 195
tmdavison Avatar answered Sep 09 '25 18:09

tmdavison