2

我有 2 个数组:

掩码:值为 0 和 1,dtype=uint8

>>> mask
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [1, 1, 1, ..., 0, 0, 0],
       ..., 
       [1, 1, 1, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)

和 prova_clip

>>> prova_clip
array([[289, 281, 271, ..., 257, 255, 255],
       [290, 284, 268, ..., 260, 260, 259],
       [294, 283, 264, ..., 265, 263, 257],
       ..., 
       [360, 359, 360, ..., 335, 338, 331],
       [359, 364, 369, ..., 337, 342, 339],
       [358, 363, 368, ..., 332, 331, 332]], dtype=uint16)

我希望使用代码保存方法用掩码屏蔽 prova_clip 以便拥有

array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [294, 283, 264, ..., 0, 0, 0],
       ..., 
       [360, 359, 360, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint16)
4

1 回答 1

5

我错过了什么吗?这看起来很简单:

prova_clip*mask

这是一个例子:

>>> a = np.arange(10,dtype=np.uint16)
>>> mask = np.ones(10,dtype=np.uint8)
>>> mask[1:3] = 0
>>> a*mask
array([0, 0, 0, 3, 4, 5, 6, 7, 8, 9], dtype=uint16)

你也可以用复杂的方式来修改数组。

>>> b = np.arange(10,dypte=np.uint16)
>>> b[~mask.astype(bool)] = 0
>>> b
array([0, 0, 0, 3, 4, 5, 6, 7, 8, 9], dtype=uint16)

最后,有numpy.where

>>> c = np.arange(10,dtype=np.uint8)
>>> np.where(mask==0,0,c)
array([0, 0, 0, 3, 4, 5, 6, 7, 8, 9], dtype=uint16)
于 2012-11-16T21:02:15.767 回答