一、实验目的
1.学会从计算和程序的角度分析问题通过完成本实验,理解计算思维,即从问题出发,通过逐步分析和分解,把原问题转化为可用程序方式解决的问题。在此过程中设计出一个解决方案。
2.进一步理解彩色空间的概念并掌握不同彩色空间转换的基本方程。
3.通过逐步设计程序,掌握编程细节:如查找表的设计,内存分配,对U和V信号进行下采样,文件读写过程等。掌握程序调试的基本方法。
二、实验原理
(1)YUV与RGB空间的相互转换
由电视原理可知,亮度和色差信号的构成如下:
Y=0.2990R+0.5870G+0.1140B
R-Y=0.7010R-0.5870G-0.1140B
B-Y=-0.2990R-0.5870G+0.8860B
为了使色差信号的动态范围控制在0.5之间,需要进行归一化,对色差信号引入压缩系数。归一化后的色差信号为:
U=-0.1684R-0.3316G+0.5B
V=0.5R-0.4187G-0.0813B
(2)码电平分配及数字表达式
⚫亮电平信号量化后码电平分配
在对分量信号进行8比特均匀量化时,共分为256个等间隔的量化级。为了防止信号变动造成过载,在256级上端留20级,下端留16级作为信号超越动态范围的保护带。
⚫色差信号量化后码电平分配
色差信号经过归一化处理后,动态范围为-0.5-0.5,让色差零电平对应码电平128,色差信号总共占225个量化级。在256级上端留15级,下端留16级作为信号超越动态范围的保护带。
(3)色度格式4:2:0格式是指色差信号U,V的取样频率为亮度信号取样频率的四分之一,在水平方向和垂直方向上的取样点数均为Y的一半。
三、实验过程
(1)rgb to yuv
参考已知代码
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <malloc.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 RGB2YUV (int x_dim, int y_dim, void *bmp, void *y_out, void *u_out, void *v_out, int flip);
void InitLookupTable();
int main(int argc, char** argv)
{
u_int frameWidth = 352;
u_int frameHeight = 240;
bool flip = TRUE;
unsigned int i;
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;
rgbFileName = argv[1];
yuvFileName = argv[2];
frameWidth = atoi(argv[3]);
frameHeight = atoi(argv[4]);
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);
}
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);
}
rgbBuf = (u_int8_t*)malloc(frameWidth * frameHeight * 3);
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);
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)yuv to rgb
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "yuv2rgb.h"
int main(int argc, const char * argv[])
{
FILE *down_rgb = NULL;
FILE *yuv = NULL;
FILE *rgb = NULL;
if ((down_rgb = fopen(argv[1], "rb")) == NULL)
{
printf("down.rgb file open fail!");
exit(0);
}
if ((yuv = fopen(argv[2], "wb")) == NULL)
{
printf("yuv file fail!");
exit(0);
}
if ((rgb = fopen(argv[5], "wb")) == NULL)
{
printf("rgb file fail!");
exit(0);
}
unsigned long picHeight, picWidth;
picHeight = atoi(argv[3]);
picWidth = atoi(argv[4]);
int buffersize;
fseek(down_rgb, 0, SEEK_END);
buffersize = ftell(down_rgb);
fseek(down_rgb, 0, SEEK_SET);
printf("down.rgb length:%d\n", buffersize);
unsigned char *buffer, *y,*u,*v;
buffer = (unsigned char *)malloc(buffersize);
y = (unsigned char *)malloc(picHeight * picWidth);
u = (unsigned char *)malloc(picHeight * picWidth);
v = (unsigned char *)malloc(picHeight * picWidth);
fread(buffer, 1, buffersize, down_rgb);
rgb2yuv(picHeight,picWidth,buffer,y,u,v,yuv);
fclose(yuv);
if ((yuv = fopen(argv[2], "rb")) == NULL)
{
printf("yuv file fail!");
exit(0);
}
int buffer2size;
fseek(yuv, 0, SEEK_END);
buffer2size = ftell(yuv);
fseek(yuv, 0, SEEK_SET);
printf("yuvFile length:%d\n", buffer2size);
unsigned char *buffer2;
int *r, *g, *b;
buffer2 = (unsigned char *)malloc(buffer2size);
r = (int *)malloc(sizeof(int)* picHeight*picWidth);
g = (int *)malloc(sizeof(int)* picHeight*picWidth);
b = (int *)malloc(sizeof(int)* picHeight*picWidth);
fread(buffer2, 1, buffer2size, yuv);
yuv2rgb(picHeight,picWidth,buffer2,r,g,b,rgb);
free(buffer);
free(y);
free(u);
free(v);
free(buffer2);
free(r);
free(g);
free(b);
fclose(down_rgb);
fclose(yuv);
fclose(rgb);
getchar();
return 0;
}
yuv2rgb.cpp
#include <stdio.h>
#include <stdlib.h>
#include "yuv2rgb.h"
void yuv2rgb(unsigned long picHeight, unsigned long picWidth, unsigned char *buffer2, int *r, int *g, int *b, FILE *rgb)
{
unsigned char *y, *u, *v,*u444,*v444;
y = (unsigned char *)malloc(picHeight * picWidth);
u = (unsigned char *)malloc(picHeight * picWidth /4);
v = (unsigned char *)malloc(picHeight * picWidth /4);
u444 = (unsigned char *)malloc(picHeight * picWidth);
v444 = (unsigned char *)malloc(picHeight * picWidth);
for (int i = 0;i < picHeight*picWidth;i++)
{
*(y + i) = *(buffer2 + i);
}
for (int i = 0;i < picHeight*picWidth/4;i++)
{
*(u + i) = *(buffer2 + i + picHeight*picWidth);
*(v + i) = *(buffer2 + i + picHeight*picWidth * 5 / 4);
}
int k = 0;
for (int i = 0;i < picHeight/2 ;i++)
{
for (int j = 0;j < picWidth/2 ;j++)
{
*(u444 + picWidth * 2 * i + 2 * j) = *(u + k);
*(u444 + picWidth * 2 * i + 2 * j + 1) = *(u + k);
*(u444 + picWidth * (2 * i + 1) + 2 * j) = *(u + k);
*(u444 + picWidth * (2 * i + 1) + 2 * j + 1) = *(u + k);
*(v444 + picWidth * 2 * i + 2 * j) = *(v + k);
*(v444 + picWidth * 2 * i + 2 * j + 1) = *(v + k);
*(v444 + picWidth * (2 * i + 1) + 2 * j) = *(v + k);
*(v444 + picWidth * (2 * i + 1) + 2 * j + 1) = *(v + k);
k++;
}
}
for (int i = 0;i < picHeight*picWidth;i++)
{
*(r + i) = int((*(y + i) * 298 + *(v444 + i) * 411 - 57344) / 255);
*(g + i) = int((*(y + i) * 298 - *(u444 + i) * 101 - *(v444 + i) * 211 + 34739) / 255);
*(b + i) = int((*(y + i) * 298 + *(u444 + i) * 519 - 71117) / 255);
if (*(r + i) < 0)
*(r + i) = 0;
if (*(r + i) > 255)
*(r + i) = 255;
if (*(g + i) < 0)
*(g + i) = 0;
if (*(g + i) > 255)
*(g + i) = 255;
if (*(b + i) < 0)
*(b + i) = 0;
if (*(b + i) > 255)
*(b + i) = 255;
}
unsigned char *rgbBuffer;
rgbBuffer = (unsigned char *)malloc(picHeight*picWidth * 3);
for (int i = 0;i < picHeight*picWidth;i++)
{
*(rgbBuffer + 3 * i) = *(b + i);
*(rgbBuffer + 3 * i + 1) = *(g + i);
*(rgbBuffer + 3 * i + 2) = *(r + i);
}
fwrite(rgbBuffer, 1, picHeight*picWidth * 3, rgb);
free(y);
free(u);
free(v);
free(u444);
free(v444);
free(rgbBuffer);
}
yuv2rgb.h
#pragma once
void rgb2yuv(unsigned long picHeight, unsigned long picWidth, unsigned char *buffer, unsigned char *y, unsigned char *u, unsigned char *v,FILE *yuv);
void yuv2rgb(unsigned long picHeight, unsigned long picWidth, unsign
ed char *buffer2, int *r, int *g, int *b, FILE *rgb);
四、实验结果