python 不同大小 numpy 数组的掩码

0 投票
1 回答
2329 浏览
提问于 2025-04-17 23:38

我有三个numpy数组:

X which is a numpy array with two dimensions (height and width), for example: 1000x2000
Y which is a numpy array with two dimensions (height and width), for example: 1000x2000
Img which is a numpy array that has three dimensions: (height, width, rgb) for example: 1000x2000x3

我创建了一个X和Y的掩码,比如说:mask = [Y[:,:]>100, X[:,:]>50],然后我把这些掩码加在一起:

masks = mask[0] & mask[1]

现在我想根据这个掩码来选择X、Y和Img的部分:

X_ = X[masks]
Y_ = Y[masks]

这样做是没问题的,但我现在也想对Img做同样的选择,不过这就不行了,因为它是一个三维数组。我该如何使用这个掩码来选择和X、Y一样的“区域”呢?

1 个回答

0

你有没有试过用同样的方法来索引它?我觉得这样应该没问题。

>>> a = arange(24).reshape(2,4,3)
>>> mask = arange(8).reshape(2,4) < 5
>>> a[mask].shape
(5, 3)

撰写回答