RGB与YUV
1、RGB转YUV
Y = 0.299 × R + 0.587 × G + 0.114 × B U = − 0.147 × R − 0.289 × G + 0.436 × B V = 0.615 × R − 0.515 × G − 0.100 × B Y=0.299×R+0.587×G+0.114×B\\ U=-0.147×R-0.289×G+0.436×B\\ V=0.615×R-0.515×G-0.100×B Y=0.299×R+0.587×G+0.114×BU=−0.147×R−0.289×G+0.436×BV=0.615×R−0.515×G−0.100×B
2、YUV转RGB
R = Y + 1.14 × V G = Y − 0.39 × U − 0.58 × V B = Y + 2.03 × U R=Y+1.14×V\\G=Y-0.39×U-0.58×V\\B=Y+2.03×U R=Y+1.14×VG=Y−0.39×U−0.58×VB=Y+2.03×U
3、将RGB24格式像素数据转换为YUV420P格式像素数据
unsigned char clip_value(unsigned char x,unsigned char min_val,unsigned char max_val)
{
if(x>max_val)
{
return max_val;
}
else if(x<min_val)
{
return min_val;
}
else
{
return x;
}
}
//RGB to YUV420
bool RGB24_TO_YUV420(unsigned char *RgbBuf,int w,int h,unsigned char *yuvBuf)
{
unsigned char*ptrY, *ptrU, *ptrV, *ptrRGB;
memset(yuvBuf,0,w*h*3/2);
ptrY = yuvBuf;
ptrU = yuvBuf + w*h;
ptrV = ptrU + (w*h*1/4);
unsigned char y, u, v, r, g, b;
for (int j = 0; j<h;j++)
{
ptrRGB = RgbBuf + w*j*3 ;
for (int i = 0;i<w;i++)
{
r = *(ptrRGB++);
g = *(ptrRGB++);
b = *(ptrRGB++);
y = (unsigned char)( ( 66 * r + 129 * g + 25 * b + 128) >> 8) + 16 ;
u = (unsigned char)( ( -38 * r - 74 * g + 112 * b + 128) >> 8) + 128 ;
v = (unsigned char)( ( 112 * r - 94 * g - 18 * b + 128) >> 8) + 128 ;
*(ptrY++) = clip_value(y,0,255);
if (j%2==0&&i%2 ==0)
{
*(ptrU++) =clip_value(u,0,255);
}
else
{
if (i%2==0)
{
*(ptrV++) =clip_value(v,0,255);
}
}
}
}
return true;
}
/*
* Convert RGB24 file to YUV420P file
* @param url_in 输入 RGB 文件的位置
* @param w 输入 RGB 文件的宽度
* @param h 输入 RGB 文件的高度
* @param num 要处理的帧数
* @param url_out 输出 YUV 文件的位置
*/
int simplest_rgb24_to_yuv420(char *url_in, int w, int h,int num,char *url_out)
{
FILE *fp=fopen(url_in,"rb+");
FILE *fp1=fopen(url_out,"wb+");
unsigned char *pic_rgb24=(unsigned char *)malloc(w*h*3);
unsigned char *pic_yuv420=(unsigned char *)malloc(w*h*3/2);
for(int i=0;i<num;i++)
{
fread(pic_rgb24,1,w*h*3,fp);
RGB24_TO_YUV420(pic_rgb24,w,h,pic_yuv420);
fwrite(pic_yuv420,1,w*h*3/2,fp1);
}
free(pic_rgb24);
free(pic_yuv420);
fclose(fp);
fclose(fp1);
return 0;
}