1

I have an image I loaded using opencv, that I would like to find pixels that are white.

input_img = [[[255,255,255], [0,127,255]],
             [[255,255,255], [255,127,255]]]

should return

white = [[1, 0],
         [1, 0]]

Is there a way to do this without reshaping or without an expensive for loop? Using something like numpy.where?

2
  • Are you looking for pure white or pixels that looks close to white? Because of noise in images, some of the pixels will not be exactly, (255, 255, 255). Perhaps post a sample test image if that is convenient. Commented Mar 14, 2014 at 7:26
  • These are white pixels - the image I receive already has noise removed (or at least I can assume that it has). Commented Mar 15, 2014 at 3:32

2 Answers 2

5

How about

(input_img == 255).all(axis=2)
Sign up to request clarification or add additional context in comments.

Comments

3

This should do it

input_img = [[[255,255,255], [0,127,255]],
         [[255,255,255], [255,127,255]]]
white = np.array(np.sum(input_img, axis=-1) == 765, dtype=np.int32)

1 Comment

maybe i got it wrong, but how come this returns this (array([0, 1]), array([0, 0]))

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.