简体   繁体   English

在不使用 OpenCV 的情况下将 YUV2 (YUYV) 帧转换为 RGB

[英]Convert YUV2 (YUYV) frames to RGB without use of OpenCV

I am trying to convert YUV2 frames generated by V4l2 to RGB.我正在尝试将 V4l2 生成的 YUV2 帧转换为 RGB。
I was able to convert YUV2 to RGB cv2.cvtColor(im, cv2.COLOR_YUV2RGB_YUYV) using OpenCV.我能够使用 OpenCV 将 YUV2 转换为 RGB cv2.cvtColor(im, cv2.COLOR_YUV2RGB_YUYV)

Currently confuse :目前混淆:
how can I convert a YUV2 frames to RGB without OpenCV.如何在没有 OpenCV 的情况下将 YUV2 帧转换为 RGB。
Also are there any examples out there?还有什么例子吗?

Referring to this page, it looks like the samples are arranged like this:参考页面,示例的排列方式如下:

start + 0:  Y'00    Cb00    Y'01    Cr00    Y'02    Cb01    Y'03    Cr01
start + 8:  Y'10    Cb10    Y'11    Cr10    Y'12    Cb11    Y'13    Cr11
start + 16: Y'20    Cb20    Y'21    Cr20    Y'22    Cb21    Y'23    Cr21
start + 24: Y'30    Cb30    Y'31    Cr30    Y'32    Cb31    Y'33    Cr31

That should mean, if your bytes are arranged in a Numpy array of this specification:这应该意味着,如果您的字节排列在本规范的 Numpy 数组中:

np.zeros((h,w), dtype=np.uint8)

you should be able to extract the Y, Cb and Cr values with:您应该能够通过以下方式提取 Y、Cb 和 Cr 值:

# Y is every row, every 2nd sample starting at 0
Y  = im[:, 0::2]
# Cb is every row, every 4th sample, starting at 1
Cb = im[:, 1::4]
# Cr is every row, every 4th sample, starting at 3
Cr = im[:, 3::4]

Then you need to resize up the Cb, Cr samples to match the width of Y.然后您需要调整 Cb、Cr 样本的大小以匹配 Y 的宽度。

Then you need to do some maths.然后你需要做一些数学运算。

YUY2 uses 4 bytes to store 2 adjacent pixels, where each pixel has a separate luminance information, but the colour information is shared between the two pixels. YUY2 使用 4 个字节存储 2 个相邻的像素,其中每个像素都有单独的亮度信息,但颜色信息在两个像素之间共享。

There are multiple definitions for the actual conversion between YUV and RGB, eg Poynton YUV和RGB之间的实际转换有多种定义,例如Poynton

Here's an integer-arithmetic implementation that I've been using for years这是我多年来一直使用的整数算术实现

# define Y_OFFSET   16
# define UV_OFFSET 128
# define YUV2RGB_11  298
# define YUV2RGB_12   -1
# define YUV2RGB_13  409
# define YUV2RGB_22 -100
# define YUV2RGB_23 -210
# define YUV2RGB_32  519
# define YUV2RGB_33    0


    while(pixelnum--) {
      int y, u, v;
      int uv_r, uv_g, uv_b;
      u=yuvdata[chU]-UV_OFFSET;
      v=yuvdata[chV]-UV_OFFSET;
      uv_r=YUV2RGB_12*u+YUV2RGB_13*v;
      uv_g=YUV2RGB_22*u+YUV2RGB_23*v;
      uv_b=YUV2RGB_32*u+YUV2RGB_33*v;

      // 1st pixel
      y=YUV2RGB_11*(yuvdata[chY0] -Y_OFFSET);
      pixels[chR] = CLAMP((y + uv_r) >> 8); // r
      pixels[chG] = CLAMP((y + uv_g) >> 8); // g
      pixels[chB] = CLAMP((y + uv_b) >> 8); // b
      pixels+=3;
      // 2nd pixel
      y=YUV2RGB_11*(yuvdata[chY1] -Y_OFFSET);
      pixels[chR] = CLAMP((y + uv_r) >> 8); // r
      pixels[chG] = CLAMP((y + uv_g) >> 8); // g
      pixels[chB] = CLAMP((y + uv_b) >> 8); // b
      pixels+=3;

      yuvdata+=4;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM