简体   繁体   中英

How to generate and apply a square mask in numpy

I really just can't grok how masks work in Numpy.

I create a mask like

import numpy
def make_mask(center_x,center_y,len_x,len_y):
      x,y = numpy.ogrid[:len_x, :len_y]
      mask = (center_x-x)**2 + (center_y-y)**2
      return mask

Then I attempt to use it

 >>>a = numpy.ones((10,10))
 >>>mask = make_mask(2,2,2,2,2)
 >>>a[mask] = 0
 >>>a
 array([[1,1,1,1,1,1,1,1],
        [1,1,1,1,1,1,1,1],
        [0,0,0,0,0,0,0,0],
        [1,1,1,1,1,1,1,1],
        [1,1,1,1,1,1,1,1],
        [0,0,0,0,0,0,0,0],
        [1,1,1,1,1,1,1,1],
        [1,1,1,1,1,1,1,1],
        [0,0,0,0,0,0,0,0]])

What I was expecting was something like

 >>>a
 array([[1,1,1,1,1,1,1,1],
        [1,1,1,1,1,1,1,1],
        [1,1,0,0,1,1,1,1],
        [1,1,0,0,1,1,1,1],
        [1,1,1,1,1,1,1,1],
        [1,1,1,1,1,1,1,1],
        [1,1,1,1,1,1,1,1],
        [1,1,1,1,1,1,1,1],
        [1,1,1,1,1,1,1,1]])

I've try a few different versions of the function. I just can't get the desired behavior. What am I doing wrong. I really don't understand how a 2D matrix indexes a 2D matrix.

Your mask is setting the 2nd, 5th and 8th rows to 0; the array is being flattened since it does not have the same shape as the array it is masking. It is being applied as:

a[2] = 0
a[5] = 0
a[8] = 0

I think you were expecting something more like:

mask = numpy.ones_like(a)
mask[center_y:center_y + len_y, center_x:center_x + len_x] = 0

which has the same size as the array you are tring to mask and gives the expected result.

If you take a look at what your make_mask function does, when written like this:

def make_mask(index_x,index_y,len_x,len_y):
      x,y = numpy.ogrid[:len_x, :len_y]
      mask = (index_x-x)**2 + (index_y-y)**2
      return mask

you will see that you get

array([[8, 5],
       [5, 2]])

and when you index a 10x10 matrix with that 2x2 matrix, I believe it treats all the values in that matrix as indexing the larger matrix by row. Which is why you see the 2nd row with all zeros, the 5th row with all zeros and the 8th row with all zeros.

To get the effect you desire, you can just use the indices you have, you don't even need a function:

a[startx:startx+lenx, starty:starty+leny] = 0

which gives:

array([[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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