我想通过opencv读取相机图像,并将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
代码运行后,在最后一行由“cvtColor”返回“ ndarray”(numpy),“显示” ndarray信息为:
>>> show.shape
(480, 640, 3)
>>> show.dtype
dtype('uint8')
>>> show.size
921600
我没有看到任何关于cv2.COLOR_BGR2RGB565,的转换代码,还有其他函数支持RGB888到RGB565吗?
或者有人知道如何将ndarray RGB888转换为RGB565?
发布于 2020-04-30 10:20:12
我认为这是正确的,但是没有任何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)
https://stackoverflow.com/questions/61519934
复制相似问题