简体   繁体   English

如何在numpy中生成和应用方形遮罩

[英]How to generate and apply a square mask in numpy

I really just can't grok how masks work in Numpy. 我真的无法理解面具在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. 我真的不明白2D矩阵如何索引2D矩阵。

Your mask is setting the 2nd, 5th and 8th rows to 0; 您的遮罩将第二行,第五行和第八行设置为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: 如果您看一下make_mask函数的功能,那么在编写时应如下所示:

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. 当您用2x2矩阵索引10x10矩阵时,我相信它将矩阵中的所有值视为按行索引较大的矩阵。 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.]])

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

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