Eric Bal
Eric Bal

Reputation: 1185

Extract Indices at Steps

I have a numpy array as shown in figure consisting of red and yellow pixels. I wan to select only the red ones. enter image description here

import numpy as np
data = np.ones((10, 10))

How it is done, guys?

Upvotes: 0

Views: 63

Answers (1)

YXD
YXD

Reputation: 32521

OK so it seems you want to mask your input with an alternation/checkerboard pattern:

import numpy as np

def checkerboard(shape):
    "A hacky way to generate a checkerboard"
    return np.sum(np.indices(shape), axis=0) % 2 == 0

data = np.ones((10, 10), dtype=np.bool)

# equivalent ways of applying the mask to your array
result = data & checkerboard(data.shape)

# or 
result = np.logical_and(data, checkerboard(data.shape))

i.e. checkerboard((5, 5)) returns

array([[ True, False,  True, False,  True],
       [False,  True, False,  True, False],
       [ True, False,  True, False,  True],
       [False,  True, False,  True, False],
       [ True, False,  True, False,  True]], dtype=bool)

Upvotes: 1

Related Questions