How can I use opencv-python to convert RGB888 to RGB565 in python?(如何在PYTHON中使用OpenCV-python将RGB888转换为RGB565?)
本文介绍了如何在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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在PYTHON中使用OpenCV-python将RGB888转换为RGB565?


猜你喜欢
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01