YUYV格式到RGB格式的转换: 为什么YUYV格式要转到RGB格式,视频的显示调用的多数API都是基于RGB格式,所以需要进行格式的转换。
YUYV格式如下:
Y0U0Y1V0 Y2U1Y3V1..........
说明:一个Y代表一个像素,而一个Y和UV组合起来构成一个像素,所以第0个像素Y0和第一个像素Y1都是共用第0个像素的U0和V0。而每个分量Y,U,V都是占用一个字节的存储空间。所以Y0U0Y1V0相当于两个像素,占用了4个字节的存储空间,平均一个像素占用两个字节。
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;
}
阅读(21387) | 评论(0) | 转发(2) |