How to index a numpy array with zeros with a boolean data type to True?

So, I recreated a Matlab project they did last year, part of which involves creating a mask that pulls out RGB stripes. They did it with an array of boolean zeros.

GMask_Whole = false(ROWS,COLS);

      

which I have restored as a numpy array.

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

      

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

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

      

I still need to find an equivalent numpy action. any ideas?

btw if you're wondering what this does: https://en.wikipedia.org/wiki/Bayer_filter

edit: things i 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

      

+3


source to share


2 answers


You can translate Matlab

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

      

for python



green_mask_whole[::2,1::2] = True

      

(assuming green_mask_whole

is a numpy array)

+1


source


numpy

can do slicing more or less than Matlab, but the syntax is slightly different. In numpy

order [begin:end:step]

, and can be left either begin

, end

or step

empty, which will give them their default values: first element, last element and step size 1 respectively.

Also, "numpy" has a nice "wide casting" system that lets you iterate over one value (or row / column) to create a new array of the same size as the other. This allows you to assign a single value to an entire array.



Thus, in the current case, you can do

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

      

+1


source







All Articles