RGB
众所周知,任何彩色图像可由不同比例的红色、绿色和蓝色组合而成,即三基色原理。这种表示彩色图像的方法即RGB彩色空间。
彩色显象管(CRT)和液晶显示器件(LCD)可显示彩色图像,彩色摄像机中的电荷耦合器件(CCD)等传感器可产生彩色电视图像,都是根据RGB原理获得的。
YCbCr(YUV)
人类视觉系统(HDV)对亮度比彩色更敏感,因此可以把亮度信息从彩色信息分离处来,并使之具有更高的清晰度,彩色信息的清晰度较低些,可显著压缩带宽,实现视频压缩的一部分,人的感觉却没有不同。
如果亮度分量用Y表示,色度用Cb,Cr表示,则由大量实验得出:
Y=0.299R+0.587G+0.114B
Cb=0.564(B-Y)
Cr=0.713(R-Y)
反之,可由下式得到相应的R、G、B:
R=Y+1.402Cr
G=Y-0.344Cb-0.714Cr
B=Y+1.772Cb
***************************************************************************************************************
YUV转RGB实例
***************************************************************************************************************
-
#include
-
#include
-
#include
-
-
void yuvtorgb(double *, unsigned char *);
-
void yuv422convertrgb(unsigned char *yuv_ptr,unsigned char *rgb_ptr,int width,int height);
-
-
int main()
-
{
-
int width = 640;
-
int height = 480;
-
int framesize = width * height;
-
unsigned char* indata = (unsigned char*) malloc (framesize*2*sizeof(char));
-
unsigned char* outdata = (unsigned char*) malloc ((framesize*3+54)*sizeof(char));
-
FILE *in,*out;
-
if((in=fopen("back.yuv","rb"))==NULL)
-
{
-
printf("can't open data.yuv");
-
return 0;
-
}
-
if ((out = fopen("back.bmp","wb")) == NULL) {
-
printf("ERROR: can't write to rgb.bmp");
-
return 0;
-
}
-
fread(indata,framesize*2,1,in);
-
yuv422convertrgb (indata,outdata,width,height);
-
fwrite(outdata,(framesize*3+54),1,out);
-
fclose(out);
-
-
free(indata);
-
free(outdata);
-
return 1;
-
-
}
-
-
void yuv422convertrgb(unsigned char *yuv_ptr,unsigned char *rgb_ptr,int width,int height)
-
{
-
int i,j,k;
-
int framesize_rgb;
-
double rgb[3];
-
unsigned char yuv[3];
-
unsigned char *prgb,*pyuv;
-
-
unsigned char bmphead[] = {
-
0x42,0x4d,0x38,0xd2,0x0f,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,
-
0x00,0x00,0xD0,0x02,0x00,0x00,0xe0,0x01,0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00,
-
0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x00,0x00,
-
0x00,0x00,0x00,0x00,0x00,0x00};
-
-
-
framesize_rgb = width * height * 3 + 54 ;
-
-
prgb = rgb_ptr;
-
for(i=0;i<54;i++)
-
*prgb++ = bmphead[i];
-
-
pyuv = yuv_ptr;
-
-
prgb = rgb_ptr + framesize_rgb - width * 3;
-
pyuv++;
-
-
for(j=0;j
-
{
-
k = 0;
-
for(i=1;i
-
{
-
if( k%2==0 )
-
{
-
yuv[0] = *pyuv--;
-
yuv[1] = *pyuv++;
-
yuv[2] = *(++pyuv);
-
pyuv++;
-
}
-
else
-
{
-
yuv[0] = *pyuv++;
-
yuv[1] = *pyuv--;
-
yuv[2] = *(--pyuv);
-
pyuv++;
-
pyuv++;
-
pyuv++;
-
}
-
yuvtorgb(rgb,yuv);
-
*prgb++ = (unsigned char )rgb[2];
-
*prgb++ = (unsigned char )rgb[1];
-
*prgb++ = (unsigned char )rgb[0];
-
}
-
prgb -= width * 3 * 2;
-
}
-
}
-
-
void yuvtorgb ( double *rgb,unsigned char *yuv)
-
{
-
int i;
-
rgb[0] = 1.0 * yuv[0] + 0 + 1.402 * (yuv[2] - 128); // r
-
rgb[1] = 1.0 * yuv[0] - 0.34413 * (yuv[1] - 128) - 0.71414 * (yuv[2]-128); // g
-
rgb[2] = 1.0 * yuv[0] + 1.772 * (yuv[1]-128) + 0; // b
-
for(i=0;i<3;i++)
-
{
-
if(rgb[i]>255)
-
rgb[i] = 255;
-
if(rgb[i]<0)
-
rgb[i] = 0;
-
}
-
}
|