如何在PYTHON中使用OpenCV-python将RGB888转换为RGB565? [英] How can I use opencv-python to convert RGB888 to RGB565 in python?

查看:32
本文介绍了如何在PYTHON中使用OpenCV-python将RGB888转换为RGB565?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过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

代码运行后,cvtColor在最后一行返回"show"ndarray(Numpy),"show"ndarray信息为:

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

我没有看到cv2.COLOR_BGR2RGB565的转换代码,有没有其他函数可以支持RGB888到RGB565?

或者有人知道如何将ndarrayRGB888转换为RGB565?

推荐答案

我认为这是正确的,但没有任何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)  

这篇关于如何在PYTHON中使用OpenCV-python将RGB888转换为RGB565?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆