YUV4:2:2转换成RGB的代码

  1. int convert_yuv_to_rgb_pixel(int y, int u, int v) 
  2. uint pixel32 = 0; 
  3. uchar *pixel = (uchar *)&pixel32; 
  4. int r, g, b; 
  5. r = y + (1.370705 * (v-128)); 
  6. g = y - (0.698001 * (v-128)) - (0.337633 * (u-128)); 
  7. b = y + (1.732446 * (u-128)); 
  8. if(r > 255) r = 255; 
  9. if(g > 255) g = 255; 
  10. if(b > 255) b = 255; 
  11. if(r < 0) r = 0; 
  12. if(g < 0) g = 0; 
  13. if(b < 0) b = 0; 
  14. pixel[0] = r * 220 / 256; 
  15. pixel[1] = g * 220 / 256; 
  16. pixel[2] = b * 220 / 256; 
  17. return pixel32; 
  18. /*yuv格式转换为rgb格式*/ 
int convert_yuv_to_rgb_pixel(int y, int u, int v)
{
 uint pixel32 = 0;
 uchar *pixel = (uchar *)&pixel32;
 int r, g, b;
 r = y + (1.370705 * (v-128));
 g = y - (0.698001 * (v-128)) - (0.337633 * (u-128));
 b = y + (1.732446 * (u-128));
 if(r > 255) r = 255;
 if(g > 255) g = 255;
 if(b > 255) b = 255;
 if(r < 0) r = 0;
 if(g < 0) g = 0;
 if(b < 0) b = 0;
 pixel[0] = r * 220 / 256;
 pixel[1] = g * 220 / 256;
 pixel[2] = b * 220 / 256;
 return pixel32;
}
/*yuv格式转换为rgb格式*/

  1. int convert_yuv_to_rgb_buffer(uchar *yuv, uchar *rgb, uint width,uint height) 
  2. uint in, out = 0; 
  3. uint pixel_16; 
  4. uchar pixel_24[3]; 
  5. uint pixel32; 
  6. int y0, u, y1, v; 
  7. for(in = 0; in < width * height * 2; in += 4) { 
  8.   pixel_16 = 
  9.    yuv[in + 3] << 24 | 
  10.    yuv[in + 2] << 16 | 
  11.    yuv[in + 1] <<  8 | 
  12.    yuv[in + 0];//YUV422每个像素2字节,每两个像素共用一个Cr,Cb值,即u和v,RGB24每个像素3个字节 
  13.   y0 = (pixel_16 & 0x000000ff); 
  14.   u  = (pixel_16 & 0x0000ff00) >>  8; 
  15.   y1 = (pixel_16 & 0x00ff0000) >> 16; 
  16.   v  = (pixel_16 & 0xff000000) >> 24; 
  17.   pixel32 = convert_yuv_to_rgb_pixel(y0, u, v); 
  18.   pixel_24[0] = (pixel32 & 0x000000ff); 
  19.   pixel_24[1] = (pixel32 & 0x0000ff00) >> 8; 
  20.   pixel_24[2] = (pixel32 & 0x00ff0000) >> 16; 
  21.   rgb[out++] = pixel_24[0]; 
  22.   rgb[out++] = pixel_24[1]; 
  23.   rgb[out++] = pixel_24[2];//rgb的一个像素 
  24.   pixel32 = convert_yuv_to_rgb_pixel(y1, u, v); 
  25.   pixel_24[0] = (pixel32 & 0x000000ff); 
  26.   pixel_24[1] = (pixel32 & 0x0000ff00) >> 8; 
  27.   pixel_24[2] = (pixel32 & 0x00ff0000) >> 16; 
  28.   rgb[out++] = pixel_24[0]; 
  29.   rgb[out++] = pixel_24[1]; 
  30.   rgb[out++] = pixel_24[2]; 
  31. return 0; 
int convert_yuv_to_rgb_buffer(uchar *yuv, uchar *rgb, uint width,uint height)
{
 uint in, out = 0;
 uint pixel_16;
 uchar pixel_24[3];
 uint pixel32;
 int y0, u, y1, v;
 for(in = 0; in < width * height * 2; in += 4) {
  pixel_16 =
   yuv[in + 3] << 24 |
   yuv[in + 2] << 16 |
   yuv[in + 1] <<  8 |
   yuv[in + 0];//YUV422每个像素2字节,每两个像素共用一个Cr,Cb值,即u和v,RGB24每个像素3个字节
  y0 = (pixel_16 & 0x000000ff);
  u  = (pixel_16 & 0x0000ff00) >>  8;
  y1 = (pixel_16 & 0x00ff0000) >> 16;
  v  = (pixel_16 & 0xff000000) >> 24;
  pixel32 = convert_yuv_to_rgb_pixel(y0, u, v);
  pixel_24[0] = (pixel32 & 0x000000ff);
  pixel_24[1] = (pixel32 & 0x0000ff00) >> 8;
  pixel_24[2] = (pixel32 & 0x00ff0000) >> 16;
  rgb[out++] = pixel_24[0];
  rgb[out++] = pixel_24[1];
  rgb[out++] = pixel_24[2];//rgb的一个像素
  pixel32 = convert_yuv_to_rgb_pixel(y1, u, v);
  pixel_24[0] = (pixel32 & 0x000000ff);
  pixel_24[1] = (pixel32 & 0x0000ff00) >> 8;
  pixel_24[2] = (pixel32 & 0x00ff0000) >> 16;
  rgb[out++] = pixel_24[0];
  rgb[out++] = pixel_24[1];
  rgb[out++] = pixel_24[2];
 }
 return 0;
}

V4L2_PIX_FMT_YUYV — Packed format with ½ horizontal chroma resolution, also known as YUV 4:2:2

In this format each four bytes is two pixels. Each four bytes is two Y's, a Cb and a Cr. Each Y goes to one of the pixels, and the Cb and Cr belong to both pixels. As you can see, the Cr and Cb components have half the horizontal resolution of the Y component. V4L2_PIX_FMT_YUYV is known in the Windows environment as YUY2.

Color Sample Location.

 0 1 2 3
0YCY YCY
1YCY YCY
2YCY YCY
3YCY YCY
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值