如何将布尔数据类型的numpy数组索引为True? [英] How do I index a numpy array of zeroes with a boolean datatype to True?

查看:584
本文介绍了如何将布尔数据类型的numpy数组索引为True?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在重新创建他们去年制作的Matlab项目,其中一部分涉及创建拉出RGB波段的蒙版。
他们用一系列逻辑零来做到这一点。

So I'm recreating a Matlab project they made last year, part of which involves creating mask that pull out the RGB bands. They did this by an array of logical zeroes.

GMask_Whole = false(ROWS,COLS);

我将其重建为numpy数组。

which I reconstructed as a numpy array.

self.green_mask_whole=np.zeros((self.rows, self.columns), dtype=bool)

下一部分,我不能为我的生活找出如何处理numpy:

The next part I can't for the life of me figure out how to do with numpy:

GMask_Whole(1:2:end,2:2:end) = true;

我还没有找到一个numpy等效动作。任何想法?

I've yet to find a numpy equivalent action. any Ideas?

btw,如果你对这是做什么感到好奇:
https://en.wikipedia.org/wiki/Bayer_filter

btw, if your curious about what this is doing: https://en.wikipedia.org/wiki/Bayer_filter

编辑:
我的事情尝试过:

edit: things I've tried:

wut(1:3:end, 1:2:end) = true
wut([1:3:end], [1:2:end]) = true
wut([1:3], [1:2]) = true
wut([1:3], [1:2]) = True
wut(slice(1:3), slice(1:2)) = True


推荐答案

numpy 可以在Matlab中做更多或更少的切片,但是synax有点不同。在 numpy 中,订单是 [begin:end:step] ,并且可以同时保留开始结束步骤为空,这将为他们提供默认值第一个元素最后一个元素步长1

numpy can do slicing more or less as in Matlab, but the synax is a little bit different. In numpy, the order is [begin:end:step] and it is possible to leave both begin, end and step empty, which will give them their default values first element, last element and step size 1 respectively.

此外,`numpy '有一个很好的'广泛投射'系统,允许重复单个值(或行/列)来创建一个与另一个相同大小的新数组。这样就可以为整个数组赋值。

Further, `numpy´ has a nice system of 'broad casting' which allows a single value (or row/column) be repeated to make a new array of the same size as another. This makes it possible to assign a single value to a whole array.

因此,在当前情况下,可以做

Thus, in the current case, it is possible to do

self.green_mask_whole=np.zeros((self.rows, self.columns), dtype=bool)
self.green_mask_whole[::2,1::2] = True

这篇关于如何将布尔数据类型的numpy数组索引为True?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆