Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenCV在Python中将RGB数组转换为YUV422_Python_Opencv - Fatal编程技术网

OpenCV在Python中将RGB数组转换为YUV422

OpenCV在Python中将RGB数组转换为YUV422,python,opencv,Python,Opencv,我需要将RGB uint8 720x480x3 numpy阵列转换为YUV422 uint8 720x480x2阵列。如何使用OpenCV实现这一点?如果OpenCV不支持它,是否还有其他Python框架支持它?OpenCV不支持您在评论中发布的RGB到YUV的转换公式 您可以使用NumPy“手动”实现转换: 将RGB(BGR)拆分为R、G和B,并转换为float 根据所述公式计算Y,U,V 水平向下采样U和V(水平调整一半大小) 圆形,剪裁为uint8范围,并转换为np.uint8类型 交错

我需要将RGB uint8 720x480x3 numpy阵列转换为YUV422 uint8 720x480x2阵列。如何使用OpenCV实现这一点?如果OpenCV不支持它,是否还有其他Python框架支持它?

OpenCV不支持您在评论中发布的RGB到YUV的转换公式

您可以使用NumPy“手动”实现转换:

  • 将RGB(BGR)拆分为R、G和B,并转换为
    float
  • 根据所述公式计算Y,U,V
  • 水平向下采样U和V(水平调整一半大小)
  • 圆形,剪裁为
    uint8
    范围,并转换为
    np.uint8
    类型
  • 交错U和V通道
  • 合并U和UV通道
下面是一个代码示例:

import numpy as np
import cv2

# Prepare BGR input (OpenCV uses BGR color ordering and not RGB):
bgr = cv2.imread('chelsea.png')
bgr = cv2.resize(bgr, (150, 100)) # Resize to even number of columns

# Split channles, and convert to float
b, g, r = cv2.split(bgr.astype(float))

rows, cols = r.shape

# Compute Y, U, V according to the formula described here:
# https://developer.apple.com/documentation/accelerate/conversion/understanding_ypcbcr_image_formats
# U applies Cb, and V applies Cr

# Use BT.709 standard "full range" conversion formula
y = 0.2126*r + 0.7152*g + 0.0722*b
u = 0.5389*(b-y) + 128
v = 0.6350*(r-y) + 128

# Downsample u horizontally
u = cv2.resize(u, (cols//2, rows), interpolation=cv2.INTER_LINEAR)

# Downsample v horizontally
v = cv2.resize(v, (cols//2, rows), interpolation=cv2.INTER_LINEAR)

# Convert y to uint8 with rounding:
y = np.round(y).astype(np.uint8)

# Convert u and v to uint8 with clipping and rounding:
u = np.round(np.clip(u, 0, 255)).astype(np.uint8)
v = np.round(np.clip(v, 0, 255)).astype(np.uint8)

# Interleave u and v:
uv = np.zeros_like(y)
uv[:, 0::2] = u
uv[:, 1::2] = v

# Merge y and uv channels
yuv422 = cv2.merge((y, uv))

OpenCV不支持您在评论中发布的RGB到YUV转换公式

您可以使用NumPy“手动”实现转换:

  • 将RGB(BGR)拆分为R、G和B,并转换为
    float
  • 根据所述公式计算Y,U,V
  • 水平向下采样U和V(水平调整一半大小)
  • 圆形,剪裁为
    uint8
    范围,并转换为
    np.uint8
    类型
  • 交错U和V通道
  • 合并U和UV通道
下面是一个代码示例:

import numpy as np
import cv2

# Prepare BGR input (OpenCV uses BGR color ordering and not RGB):
bgr = cv2.imread('chelsea.png')
bgr = cv2.resize(bgr, (150, 100)) # Resize to even number of columns

# Split channles, and convert to float
b, g, r = cv2.split(bgr.astype(float))

rows, cols = r.shape

# Compute Y, U, V according to the formula described here:
# https://developer.apple.com/documentation/accelerate/conversion/understanding_ypcbcr_image_formats
# U applies Cb, and V applies Cr

# Use BT.709 standard "full range" conversion formula
y = 0.2126*r + 0.7152*g + 0.0722*b
u = 0.5389*(b-y) + 128
v = 0.6350*(r-y) + 128

# Downsample u horizontally
u = cv2.resize(u, (cols//2, rows), interpolation=cv2.INTER_LINEAR)

# Downsample v horizontally
v = cv2.resize(v, (cols//2, rows), interpolation=cv2.INTER_LINEAR)

# Convert y to uint8 with rounding:
y = np.round(y).astype(np.uint8)

# Convert u and v to uint8 with clipping and rounding:
u = np.round(np.clip(u, 0, 255)).astype(np.uint8)
v = np.round(np.clip(v, 0, 255)).astype(np.uint8)

# Interleave u and v:
uv = np.zeros_like(y)
uv[:, 0::2] = u
uv[:, 1::2] = v

# Merge y and uv channels
yuv422 = cv2.merge((y, uv))

请看,您能否解释为什么预期结果为720x480x2?它与macOS特定格式KCMVideocateType_422YPCBCCR8(CMVideoCodeType)相关请看,您能否解释为什么预期结果为720x480x2?它与macOS特定格式KCMVideocateType_422YPCBCCR8(CMVideoCodeType)相关