1

I have an image as a matrix (ndarray) of shape (720, 1280, 3) with RGB pixels (variable: original) and another ndarray of shape (720, 1280) which is made of booleans (variable: im).

for every True value in im I want original's corresponding pixel to be of color [0, 0, 255].

I tried (with both arrays flattened)

for i in range(im.size):
  if(im[i] == True):
    original[i] = [0, 0, 255]

but its too slow to have it as a video output.

Any ideas how I can speed this up?

1
  • 1
    From array's perspective, code (1D) and comments (2D) don't match. Commented Apr 30, 2021 at 19:19

1 Answer 1

5

This is the same as doing (without flattening your arrays):

original[im == True] = [0, 0, 255]

That is the vectorized representation of your for loop.

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

1 Comment

Or just original[im] = [0,0,255].

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.