2

I would like to read camera image by opencv-python and send image raw data (byte array) in RGB565 format to device. Here are some testing codes:

import cv2
cam = cv2.VideoCapture(0) # open camera
flag, image = cam.read() # read image from camera
show = cv2.resize(image, (640, 480)) # resize to 640x480
show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB) # convert to RGB888

After codes run, it returned "show" ndarray (numpy) by cvtColor at last line, the "show" ndarray info is:

>>> show.shape
(480, 640, 3)
>>> show.dtype
dtype('uint8')
>>> show.size
921600

I don't see any convert code about cv2.COLOR_BGR2RGB565, is there any other function to support RGB888 to RGB565?

Or someone knows how to convert ndarray RGB888 to RGB565?

2 Answers 2

2

I think this is right but don't have anything RGB565 to test it on:

#!/usr/bin/env python3

import numpy as np

# Get some deterministic randomness and synthesize small image
np.random.seed(42)
im = np.random.randint(0,256,(1,4,3), dtype=np.uint8)

# In [67]: im
# Out[67]:
# array([[[102, 220, 225],
#        [ 95, 179,  61],
#        [234, 203,  92],
#        [  3,  98, 243]]], dtype=uint8)

# Make components of RGB565
R5 = (im[...,0]>>3).astype(np.uint16) << 11
G6 = (im[...,1]>>2).astype(np.uint16) << 5
B5 = (im[...,2]>>3).astype(np.uint16)

# Assemble components into RGB565 uint16 image
RGB565 = R5 | G6 | B5

# Produces this:
# array([[26364, 23943, 61003,   798]], dtype=uint16)

Or, you can remove your cv2.cvtColor(show, cv2.COLOR_BGR2RGB) and swap the indices to:

R5 = (im[...,2]>>3).astype(np.uint16) << 11
G6 = (im[...,1]>>2).astype(np.uint16) << 5
B5 = (im[...,0]>>3).astype(np.uint16)  
Sign up to request clarification or add additional context in comments.

4 Comments

By the way Mark, may I ask if my code below is correct to transfer RGB565 to byte array or not? raw = RGB565.tobytes()
Thanks Mark, I'm just new to python, may ask some basic questions frequently. ^^
It's ok... we're all here to learn. And questions are free, same as answers! :-)
Hi Mark, I have the similar question about RGB565 to RGB888 with testing code, but I got stuck...XD, please help me if you got time. ^^ Please refer to stackoverflow.com/questions/61816430/….
1

Why not use the usual cvtColor() function from OpenCV? I see enums such as COLOR_BGR2BGR565, COLOR_BGR5652BGR, COLOR_BGR5652RGB, and COLOR_RGBA2BGR565 among others. Wouldn't it be better to use OpenCV to do the conversion vs writing your own?

2 Comments

Hi Stephane, it's 2 years ago, I almost forget why I ask this question. I re-check the enums and found there is no enum to convert RGB888 to RGB565, that's why I ask this question. The purpose I need it is because I need RGB565 format data to be sent to my device for inference. If I use COLOR_BGR2BGR565, I still need a function to convert BGR565 to RGB565, so if I can get the answer directly from RGB888 to RGB565, then it's more convinient :)
I didn't know opencv not provide RGB565 colorspace 4 years ago.

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.