不使用OpenCV,将YUV2(YUYV)框架转换为RGB

发布于 2025-01-25 10:15:19 字数 183 浏览 3 评论 0原文

我正在尝试将V4L2生成的YUV2帧转换为RGB。
我能够使用openCV将YUV2转换为rgb cv2.cvtcolor(im,cv2.color_yuv2rgb_yuyv)使用OpenCV。

当前混淆:
在没有OPENCV的情况下,如何将YUV2帧转换为RGB。
还有什么例子吗?

I am trying to convert YUV2 frames generated by V4l2 to RGB.
I was able to convert YUV2 to RGB cv2.cvtColor(im, cv2.COLOR_YUV2RGB_YUYV) using OpenCV.

Currently confuse :
how can I convert a YUV2 frames to RGB without OpenCV.
Also are there any examples out there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疾风者 2025-02-01 10:15:19

参考 ,看起来样本是这样的:

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

这应该意味着,如果您的字节在此规范的数组中排列,

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

则应使用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]

然后您需要调整大小CB,CR样品以匹配Y的宽度。

然后您需要进行一些数学。

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:

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

you should be able to extract the Y, Cb and Cr values with:

# 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.

Then you need to do some maths.

瘫痪情歌 2025-02-01 10:15:19

Yuy2使用4个字节来存储2个相邻像素,每个像素都有单独的亮度信息,但是两个像素之间共享颜色信息。

YUV和RGB之间的实际转换有多个定义,例如

这是我多年来一直使用的整数实现

# 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;
    }

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.

There are multiple definitions for the actual conversion between YUV and RGB, e.g. 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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文