一、实验要求
1. 基本要求(必做)
编写RGB转化为YUV程序,重点掌握函数定义,部分查找表的初始化和调用,缓冲区分配。将得到的RGB文件转换为YUV文件,用YUV Viewer播放器观看,验证是否正确。
2. 提高要求(可选)
编写将YUV转换为RGB的程序。将给定的实验数据用该程序转换为RGB文件。并与原RGB文件进行比较,如果有误差,分析误差来自何处。
注:down.rgb和down.yuv两个文件的分辨率均为256*256。yuv文件为4:2:0采样空间。
二、实验思路
1. 创建1个头文件、2个源程序文件,在主程序中设置初始化参数
定义输入、输出文件指针,定义宽、高变量并赋值,定义四个缓冲区指针。
2. 打开待转换的文件
为rgb、y、u、v(或yuv、r、g、b)四个缓冲区指针分配相应大小的动态内存,用fread函数将rgb文件(或yuv文件)的数据依次读入四个缓冲区。
3. 调用自定义的RGB2YUV(或YUV2RGB)函数
先调用查找表计算相应像素的Y、U、V值,再进行下采样,输出到yuv的缓冲区。
(或者先对U、V分量进行上采样,再调用查找表计算相应像素的R、G、B值,输出到rgb的缓冲区。)
-
查找表
由于从内存中提取数值经常要比复杂的计算速度快很多,所以使用查找表得到的速度提升是很显著的。 -
RGB 与 YUV 空间的相互转换
-
转换公式
为将两个色差信号电平控制在-0.5~0.5,零电平对应128的码电平Y = 0.299 R + 0.587 G + 0.114 B U = - 0.1684 R - 0.3316 G + 0.5 B V = 0.5 R - 0.4187 G - 0.0813 B R = Y + 1.4075 *(V-128) G = Y – 0.3455 (U –128) – 0.7169 (V –128) B = Y + 1.779 *(U – 128)
-
越界判定
程序YUV2RGB,计算出的RGB值会超过允许的范围(0-255),会导致图像失真。因此需要舍去超过范围的RGB值,若>255则赋值为255,若<0则赋值为0。
4. 将数据写入文件,释放缓冲区,关闭文件。
5. 在调试中设置程序文件所在的工作目录和主函数所需的命令参数
命令参数分别为:读入待转换的文件名、输出文件名、图像的宽、图像的高。
三、代码实现
1.RGB2YUV
- 头文件(rgb2yuv.h)
int RGB2YUV (int x_dim, int y_dim, void *bmp, void *y_out, void *u_out, void *v_out, int flip);
void InitLookupTable();
- 源文件(main.cpp)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "rgb2yuv.h"
#define u_int8_t unsigned __int8
#define u_int unsigned __int32
#define u_int32_t unsigned __int32
#define FALSE false
#define TRUE true
int main(int argc, char** argv)
{
/* variables controlable from command line */
u_int frameWidth = 352; /* --width=<uint> */
u_int frameHeight = 240; /* --height=<uint> */
bool flip = TRUE; /* --flip */
unsigned int i;
/* internal variables */
char* rgbFileName = NULL;
char* yuvFileName = NULL;
FILE* rgbFile = NULL;
FILE* yuvFile = NULL;
u_int8_t* rgbBuf = NULL;
u_int8_t* yBuf = NULL;
u_int8_t* uBuf = NULL;
u_int8_t* vBuf = NULL;
u_int32_t videoFramesWritten = 0;
/* begin process command line */
/* point to the specified file names */
rgbFileName = argv[1];
yuvFileName = argv[2];
frameWidth = atoi(argv[3]);
frameHeight = atoi(argv[4]);
/* open the RGB file */
rgbFile = fopen(rgbFileName, "rb");
if (rgbFile == NULL)
{
printf("cannot find rgb file\n");
exit(1);
}
else
{
printf("The input rgb file is %s\n", rgbFileName);
}
/* open the RAW file */
yuvFile = fopen(yuvFileName, "wb");
if (yuvFile == NULL)
{
printf("cannot find yuv file\n");
exit(1);
}
else
{
printf("The output yuv file is %s\n", yuvFileName);
}
/* get an input buffer for a frame */
rgbBuf = (u_int8_t*)malloc(frameWidth * frameHeight * 3);
/* get the output buffers for a frame */
yBuf = (u_int8_t*)malloc(frameWidth * frameHeight);
uBuf = (u_int8_t*)malloc((frameWidth * frameHeight) / 4);
vBuf = (u_int8_t*)malloc((frameWidth * frameHeight) / 4);
if (rgbBuf == NULL || yBuf == NULL || uBuf == NULL || vBuf == NULL)
{
printf("no enought memory\n");
exit(1);
}
while (fread(rgbBuf, 1, frameWidth * frameHeight * 3, rgbFile))
{
if(RGB2YUV(frameWidth, frameHeight, rgbBuf, yBuf, uBuf, vBuf, flip))
{
printf("error");
return 0;
}
for (i = 0; i < frameWidth*frameHeight; i++)
{
if (yBuf[i] < 16) yBuf[i] = 16;
if (yBuf[i] > 235) yBuf[i] = 235;
}
for (i = 0; i < frameWidth*frameHeight/4; i++)
{
if (uBuf[i] < 16) uBuf[i] = 16;
if (uBuf[i] > 240) uBuf[i] = 240;
if (vBuf[i] < 16) vBuf[i] = 16;
if (vBuf[i] > 240) vBuf[i] = 240;
}
fwrite(yBuf, 1, frameWidth * frameHeight, yuvFile);
fwrite(uBuf, 1, (frameWidth * frameHeight) / 4, yuvFile);
fwrite(vBuf, 1, (frameWidth * frameHeight) / 4, yuvFile);
printf("\r...%d", ++videoFramesWritten);
}
printf("\n%u %ux%u video frames written\n",
videoFramesWritten, frameWidth, frameHeight);
/* cleanup */
fclose(rgbFile);
fclose(yuvFile);
return(0);
}
- 源文件(rgb2yuv.cpp)
#include "stdlib.h"
#include "rgb2yuv.h"
static float RGBYUV02990[256], RGBYUV05870[256], RGBYUV01140[256];
static float RGBYUV01684[256], RGBYUV03316[256];
static float RGBYUV04187[256], RGBYUV00813[256];
int RGB2YUV (int x_dim, int y_dim, void *bmp, void *y_out, void *u_out, void *v_out, int flip)
{
static int init_done = 0;
long i, j, size;
unsigned char *r, *g, *b;
unsigned char *y, *u, *v;
unsigned char *pu1, *pu2, *pv1, *pv2, *psu, *psv;
unsigned char *y_buffer, *u_buffer, *v_buffer;
unsigned char *sub_u_buf, *sub_v_buf;
if (init_done == 0)
{
InitLookupTable();
init_done = 1;
}
// check to see if x_dim and y_dim are divisible by 2
if ((x_dim % 2) || (y_dim % 2)) return 1;
size = x_dim * y_dim;
// allocate memory
y_buffer = (unsigned char *)y_out;
sub_u_buf = (unsigned char *)u_out;
sub_v_buf = (unsigned char *)v_out;
u_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
v_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
if (!(u_buffer && v_buffer))
{
if (u_buffer) free(u_buffer);
if (v_buffer) free(v_buffer);
return 2;
}
b = (unsigned char *)bmp;
y = y_buffer;
u = u_buffer;
v = v_buffer;
// convert RGB to YUV
if (!flip) {
for (j = 0; j < y_dim; j ++)
{
y = y_buffer + (y_dim - j - 1) * x_dim;
u = u_buffer + (y_dim - j - 1) * x_dim;
v = v_buffer + (y_dim - j - 1) * x_dim;
for (i = 0; i < x_dim; i ++) {
g = b + 1;
r = b + 2;
*y = (unsigned char)( RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
*u = (unsigned char)(- RGBYUV01684[*r] - RGBYUV03316[*g] + (*b)/2 + 128);
*v = (unsigned char)( (*r)/2 - RGBYUV04187[*g] - RGBYUV00813[*b] + 128);
b += 3;
y ++;
u ++;
v ++;
}
}
} else {
for (i = 0; i < size; i++)
{
g = b + 1;
r = b + 2;
*y = (unsigned char)( RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
*u = (unsigned char)(- RGBYUV01684[*r] - RGBYUV03316[*g] + (*b)/2 + 128);
*v = (unsigned char)( (*r)/2 - RGBYUV04187[*g] - RGBYUV00813[*b] + 128);
b += 3;
y ++;
u ++;
v ++;
}
}
// subsample UV
for (j = 0; j < y_dim/2; j ++)
{
psu = sub_u_buf + j * x_dim / 2;
psv = sub_v_buf + j * x_dim / 2;
pu1 = u_buffer + 2 * j * x_dim;
pu2 = u_buffer + (2 * j + 1) * x_dim;
pv1 = v_buffer + 2 * j * x_dim;
pv2 = v_buffer + (2 * j + 1) * x_dim;
for (i = 0; i < x_dim/2; i ++)
{
*psu = (*pu1 + *(pu1+1) + *pu2 + *(pu2+1)) / 4;
*psv = (*pv1 + *(pv1+1) + *pv2 + *(pv2+1)) / 4;
psu ++;
psv ++;
pu1 += 2;
pu2 += 2;
pv1 += 2;
pv2 += 2;
}
}
free(u_buffer);
free(v_buffer);
return 0;
}
void InitLookupTable()
{
int i;
for (i = 0; i < 256; i++) RGBYUV02990[i] = (float)0.2990 * i;
for (i = 0; i < 256; i++) RGBYUV05870[i] = (float)0.5870 * i;
for (i = 0; i < 256; i++) RGBYUV01140[i] = (float)0.1140 * i;
for (i = 0; i < 256; i++) RGBYUV01684[i] = (float)0.1684 * i;
for (i = 0; i < 256; i++) RGBYUV03316[i] = (float)0.3316 * i;
for (i = 0; i < 256; i++) RGBYUV04187[i] = (float)0.4187 * i;
for (i = 0; i < 256; i++) RGBYUV00813[i] = (float)0.0813 * i;
}
2.YUV2RGB
- 头文件(yuv2rgb.h)
int YUV2RGB(int x_dim, int y_dim, void* bmp, void* y_out, void* u_out, void* v_out, int flip);
void InitLookupTable();
- 源文件(main.cpp)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "yuv2rgb.h"
#define u_int8_t unsigned __int8
#define u_int unsigned __int32
#define u_int32_t unsigned __int32
#define FALSE false
#define TRUE true
int main(int argc, char* argv[])
{
/* variables controlable from command line */
u_int frameWidth = 352; /* --width=<uint> */
u_int frameHeight = 240; /* --height=<uint> */
bool flip = TRUE; /* --flip */
unsigned int i;
/* internal variables */
char* rgbFileName = NULL;
char* yuvFileName = NULL;
FILE* rgbFile = NULL;
FILE* yuvFile = NULL;
u_int8_t* rgbBuf = NULL;
u_int8_t* yBuf = NULL;
u_int8_t* uBuf = NULL;
u_int8_t* vBuf = NULL;
u_int32_t videoFramesWritten = 0;
/* begin process command line */
/* point to the specified file names */
yuvFileName = argv[1];
rgbFileName = argv[2];
frameWidth = atoi(argv[3]);
frameHeight = atoi(argv[4]);
/* open the YUV file */
yuvFile = fopen(yuvFileName, "rb");
if (yuvFile == NULL)
{
printf("cannot find yuv file\n");
exit(1);
}
else
{
printf("The input yuv file is %s\n", yuvFileName);
}
/* open the RGB file */
rgbFile = fopen(rgbFileName, "wb");
if (rgbFile == NULL)
{
printf("cannot find rgb file\n");
exit(1);
}
else
{
printf("The output rgb file is %s\n", rgbFileName);
}
/* get an input buffer for a frame */
rgbBuf = (u_int8_t*)malloc(frameWidth * frameHeight * 3);
/* get the output buffers for a frame */
yBuf = (u_int8_t*)malloc(frameWidth * frameHeight);
uBuf = (u_int8_t*)malloc((frameWidth * frameHeight) / 4);
vBuf = (u_int8_t*)malloc((frameWidth * frameHeight) / 4);
if (rgbBuf == NULL || yBuf == NULL || uBuf == NULL || vBuf == NULL)
{
printf("no enought memory\n");
exit(1);
}
while (fread(yBuf, 1, frameWidth * frameHeight, yuvFile)
&& fread(uBuf, 1, frameWidth * frameHeight / 4, yuvFile)
&& fread(vBuf, 1, frameWidth * frameHeight / 4, yuvFile))
{
if (YUV2RGB(frameWidth, frameHeight, rgbBuf, yBuf, uBuf, vBuf, flip))
{
printf("error");
return 0;
}
fwrite(rgbBuf, 1, frameWidth * frameHeight * 3, rgbFile);
printf("\r...%d", ++videoFramesWritten);
}
printf("\n%u %ux%u video frames written\n",
videoFramesWritten, frameWidth, frameHeight);
fclose(rgbFile);
fclose(yuvFile);
if (rgbBuf != NULL) free(rgbBuf);
if (yBuf != NULL)free(yBuf);
if (uBuf != NULL)free(uBuf);
if (vBuf != NULL)free(vBuf);
return(0);
}
- 源文件(yuv2rgb.cpp)
#include "stdlib.h"
#include "yuv2rgb.h"
static float YUVRGB14075[256];
static float YUVRGB03455[256], YUVRGB07169[256];
static float YUVRGB1779[256];
int YUV2RGB(int width, int height, void* rgb_out, void* y_in, void* u_in, void* v_in, int flip)
{
static int init_done = 0;
long i, j, size;
float r1, g1, b1;
unsigned char* r, * g, * b;
unsigned char* y, * u, * v;
unsigned char* y_buffer, * u_buffer, * v_buffer, * rgb_buffer;
unsigned char* sub_u_buf, * sub_v_buf;
if (init_done == 0)
{
InitLookupTable();
init_done = 1;
}
if ((width % 2) || (height % 2)) return 1;
size = width * height;
y_buffer = (unsigned char*)y_in;
u_buffer = (unsigned char*)u_in;
v_buffer = (unsigned char*)v_in;
rgb_buffer = (unsigned char*)rgb_out;
sub_u_buf = (unsigned char*)malloc(size);
sub_v_buf = (unsigned char*)malloc(size);
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
*(sub_u_buf + i * width + j) = *(u_buffer + (i / 2) * (width / 2) + j / 2);
*(sub_v_buf + i * width + j) = *(v_buffer + (i / 2) * (width / 2) + j / 2);
}
}
b = rgb_buffer;//RGB格式文件储存时顺序为倒序
y = y_buffer;
u = sub_u_buf;
v = sub_v_buf;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
g = b + 1;
r = b + 2;
r1 = *y + YUVRGB14075[*v];
g1 = *y - YUVRGB03455[*u] - YUVRGB07169[*v];
b1 = *y + YUVRGB1779[*u];
*r = (r1 > 0 ? (r1 > 255 ? 255 : (unsigned char)r1) : 0);
*g = (g1 > 0 ? (g1 > 255 ? 255 : (unsigned char)g1) : 0);
*b = (b1 > 0 ? (b1 > 255 ? 255 : (unsigned char)b1) : 0);
b = b + 3;
y++;
u++;
v++;
}
}
if (sub_u_buf != NULL) free(sub_u_buf);
if (sub_v_buf != NULL) free(sub_v_buf);
return 0;
}
void InitLookupTable()
{
int i;
for (i = 0; i < 256; i++) YUVRGB14075[i] = (float)1.4075 * (i - 128);
for (i = 0; i < 256; i++) YUVRGB03455[i] = (float)0.3455 * (i - 128);
for (i = 0; i < 256; i++) YUVRGB07169[i] = (float)0.7169 * (i - 128);
for (i = 0; i < 256; i++) YUVRGB1779[i] = (float)1.779 * (i - 128);
}
四、总结分析
1.RGB2YUV
左图为yuv原图,右图为rgb图像转换的yuv图像。可以看出图像一致,转换成功。
经理论分析,图像转换无损。
2.YUV2RGB
左图为rgb原图,右图为yuv图像转换的rgb图像。可以看出并无明显误差,但经理论分析转换有损。
误差分析
- 因为YUV采样空间为4:2:0,所以U、V分量只有Y的1/4。在YUV2RGB的转换过程中,需要进行上采样,因此造成了误差。
- 公式只保留了四位小数,造成了计算误差。