3

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);

which I reconstructed as a numpy array.

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

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;

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

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
1
  • editing my original post. Commented Jun 19, 2017 at 18:48

2 Answers 2

1

You can translate Matlab's

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

to python by

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

(assuming green_mask_whole is a numpy array)

Sign up to request clarification or add additional context in comments.

Comments

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.