Как я могу использовать opencv- python для преобразования RGB888 в RGB565 в python? - PullRequest
2 голосов
/ 30 апреля 2020

Я хотел бы прочитать изображение камеры с помощью opencv- python и отправить необработанные данные изображения (байтовый массив) в формате RGB565 на устройство. Вот несколько тестовых кодов:

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

После выполнения кодов он вернул "show" ndarray (numpy) cvtColor в последней строке, "show" информация ndarray:

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

Я не вижу никакого кода преобразования для cv2.COLOR_BGR2RGB 565 , есть ли какая-либо другая функция для поддержки RGB888 в RGB565?

Или кто-то знает, как конвертировать ndarray RGB888 в RGB565?

1 Ответ

2 голосов
/ 30 апреля 2020

Я думаю, что это правильно, но у меня нет RGB565 для проверки:

#!/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)

Или вы можете удалить cv2.cvtColor(show, cv2.COLOR_BGR2RGB) и поменять индексы на:

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