在numpy中如何将二维数组中的行切片归零?

2024-04-18 01:13:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表示图像的numpy数组。我想将每列中低于某一行的所有索引归零(基于外部数据)。我似乎不知道如何分割/广播/安排数据来做到这一点。你知道吗

def first_nonzero(arr, axis, invalid_val=-1):
    mask = arr!=0
    return np.where(mask.any(axis=axis), mask.argmax(axis=axis), invalid_val)

# Find first non-zero pixels in a processed image
# Note, I might have my axes switched here... I'm not sure.
rows_to_zero = first_nonzero(processed_image, 0, processed_image.shape[1])

# zero out data in image below the rows found
# This is the part I'm stuck on.
image[:, :rows_to_zero, :] = 0  # How can I slice along an array of indexes?

# Or in plain python, I'm trying to do this:
for x in range(image.shape[0]):
    for y in range(rows_to_zero, image.shape[1]):
        image[x,y] = 0

Tags: to数据inimagemaskvalrowsfirst
1条回答
网友
1楼 · 发布于 2024-04-18 01:13:17

使用^{}创建掩码并分配-

mask = rows_to_zero <= np.arange(image.shape[0])[:,None]
image[mask] = 0

或与反转掩码相乘:image *= ~mask。你知道吗

示例运行到showcase掩码设置-

In [56]: processed_image
Out[56]: 
array([[1, 0, 1, 0],
       [1, 0, 1, 1],
       [0, 1, 1, 0],
       [0, 1, 0, 1],
       [1, 1, 1, 1],
       [0, 1, 0, 1]])

In [57]: rows_to_zero
Out[57]: array([0, 2, 0, 1])

In [58]: rows_to_zero <= np.arange(processed_image.shape[0])[:,None]
Out[58]: 
array([[ True, False,  True, False],
       [ True, False,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]], dtype=bool)

另外,对于每列的设置,我认为您的意思是:

rows_to_zero = first_nonzero(processed_image, 0, processed_image.shape[0]-1)

如果您打算在每行的基础上进行归零,那么每行的索引将是第一个非零索引,我们称之为idx。所以,那就-

mask = idx[:,None] <= np.arange(image.shape[1])
image[mask] = 0

样本运行-

In [77]: processed_image
Out[77]: 
array([[1, 0, 1, 0],
       [1, 0, 1, 1],
       [0, 1, 1, 0],
       [0, 1, 0, 1],
       [1, 1, 1, 1],
       [0, 1, 0, 1]])

In [78]: idx = first_nonzero(processed_image, 1, processed_image.shape[1]-1)

In [79]: idx
Out[79]: array([0, 0, 1, 1, 0, 1])

In [80]: idx[:,None] <= np.arange(image.shape[1])
Out[80]: 
array([[ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [False,  True,  True,  True],
       [False,  True,  True,  True],
       [ True,  True,  True,  True],
       [False,  True,  True,  True]], dtype=bool)

相关问题 更多 >

    热门问题