• 博客访问： 474448
• 博文数量： 98
• 博客积分： 3265
• 博客等级： 中校
• 技术积分： 1227
• 用 户 组： 普通用户
• 注册时间： 2010-10-23 00:29

2012年（6）

2011年（83）

2010年（9）

2012-06-19 22:36:20

YUYV格式到RGB格式的转换：

YUYV格式如下：
Y0U0Y1V0 Y2U1Y3V1..........

RGB格式：
R0G0B0 R1G1B1.........

YUV到RGB的转换有如下公式：
R = 1.164*(Y-16) + 1.159*(V-128);
G = 1.164*(Y-16) - 0.380*(U-128)+ 0.813*(V-128);
B = 1.164*(Y-16) + 2.018*(U-128));

int yuvtorgb0(unsigned char *yuv, unsigned char *rgb, unsigned int
width, unsigned int height)
{
unsigned int in, out;
int y0, u, y1, v;
unsigned int pixel24;
unsigned char *pixel = (unsigned char *)&pixel24;
unsigned int size = width*height*2;

for(in = 0, out = 0; in < size; in += 4, out += 6)
{
y0 = yuv[in+0];
u  = yuv[in+1];
y1 = yuv[in+2];
v  = yuv[in+3];

sign3 = true;
pixel24 = yuvtorgb(y0, u, v);
rgb[out+0] = pixel[0];    //for QT
rgb[out+1] = pixel[1];
rgb[out+2] = pixel[2];
//rgb[out+0] = pixel[2];  //for iplimage
//rgb[out+1] = pixel[1];
//rgb[out+2] = pixel[0];

//sign3 = true;
pixel24 = yuvtorgb(y1, u, v);
rgb[out+3] = pixel[0];
rgb[out+4] = pixel[1];
rgb[out+5] = pixel[2];
//rgb[out+3] = pixel[2];
//rgb[out+4] = pixel[1];
//rgb[out+5] = pixel[0];
}
return 0;
}

int yuvtorgb(int y, int u, int v)
{
unsigned int pixel24 = 0;
unsigned char *pixel = (unsigned char *)&pixel24;
int r, g, b;
static long int ruv, guv, buv;

if(sign3)
{
sign3 = false;
ruv = 1159*(v-128);
guv = 380*(u-128) + 813*(v-128);
buv = 2018*(u-128);
}

r = (1164*(y-16) + ruv) / 1000;
g = (1164*(y-16) - guv) / 1000;
b = (1164*(y-16) + buv) / 1000;

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;
pixel[1] = g;
pixel[2] = b;

return pixel24;
}

0