Charles L.
Charles L.

Reputation: 6275

Using opencv / Numpy to find white pixels in a color image using python

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?

Upvotes: 1

Views: 4182

Answers (2)

YXD
YXD

Reputation: 32511

How about

(input_img == 255).all(axis=2)

Upvotes: 5

vehsakul
vehsakul

Reputation: 1158

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)

Upvotes: 3

Related Questions