Ask Your Question

How to convert an RGB image to Boolean array?

asked 2019-08-12 07:38:16 -0500

SagRU gravatar image


I'm totally new to OpenCV and NumPy. When I load an image using cv2.imread, I get a NumPy array with RGBs inside, so every pixel is described as [B G R]. I want to convert this array to boolean one, where every pixel is either black (0) or white (1).

Are there any built-in OpenCV functions for this task?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-08-12 09:34:01 -0500

berak gravatar image
  1. load the image as grayscale,single channel intensity image, not as a color one:

    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)

  2. binarize it (the opencv way). you can use cv2.threshold ,, or similar functions, resulting in an np.uint8 array, where each "on" pixel is 255, and each "off" one is 0.

  3. if you really need binary (True/False) values, you can use numpy operations, like: bin = img > 20 , just note, that those are unusable for further opencv processing (where [0..255] is expected)

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-08-12 07:38:16 -0500

Seen: 2,184 times

Last updated: Aug 12 '19