0

I have numpy array a of shape m,n,3 and mask of shape m,n. When I try setting a[mask > 0] = (255, 0, 0) I get error

ValueError: NumPy boolean array indexing assignment cannot assign 3 input values to the 7401 output values where the mask is true`

How can I achieve this functionality in numpy?

3
  • Try numpy.ma Commented Aug 22, 2019 at 9:06
  • 1
    I cannot reproduce the issue you are seeing. Commented Aug 22, 2019 at 9:07
  • sorry, my mistake. I was indexing with mask that was m,n,3 shaped Commented Aug 22, 2019 at 10:27

1 Answer 1

1
a = np.random.randint(0,2,size=(100,200,3))
mask = np.random.randint(0,2,size=(100,200))
a[mask > 0] = [255,0,0]
# Test
assert np.equal(a[mask > 0], 
    np.ones_like(a[mask > 0])*[255,0,0]).sum() == np.multiply(*a[mask > 0].shape)
Sign up to request clarification or add additional context in comments.

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.