彩色空间之间的转换实验报告

这篇实验报告详细探讨了RGB到YUV以及YUV到RGB的颜色空间转换原理,通过编写和实现代码,包括rgb2yuv.h、rgb2yuv.cpp、yuv2rgb.h、yuv2rgb.cpp及main.cpp,对转换过程进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.实验原理
二.代码实现

1.RGB转YUV实验
(1)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();

(2)rgb2yuv.cpp

  • 实现将rgb数据转化为yuv数据。
#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];//定义查找表所用公式系数的浮点型数组,且数组大小为量化级范围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;//定义调用查找表时的控制变量,同时初始化为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;//定义buffer指针
	unsigned char *sub_u_buf, *sub_v_buf;////定义上采样后的U、V缓冲区指针

	if (init_done == 0)
	{
		InitLookupTable();//调用查找表,计算各量化级的公式系数
		init_done = 1;
	}
	if ((x_dim % 2) || (y_dim % 2)) return 1;//判断图像宽、高是否为偶数,从而确定上采样格式
	size = x_dim * y_dim;//尺寸变量赋值

	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));//为上采样后的U、V缓冲区指针分配动态内存
	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;
	
	//  RGB 转 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;//RGB文件中图像每一像素按B、G、R依次排列
				*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 ++;
		}
	}

	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;
}

(3)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)
{
	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]);

	fopen_s(&rgbFile,"down.rgb", "rb");//打开.rgb格式文件
	if (rgbFile == NULL)
	{
		printf("cannot find rgb file\n");
		exit(1);
	}
	else
	{
		printf("The input rgb file is %s\n", rgbFileName);
	}

	fopen_s(&yuvFile, "down.yuv", "wb");//创建.yuv格式文件
	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);
	
	//用fread函数将yuvFile的数据依次读入yBuf、uBuf、vBuf
	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;
		}
		//调用YUV2RGB函数后,再将r、g、bBuf的数据写入rgbFile中
		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);
}

2.YUV转RGB实验
(1) yuv2rgb.h

int YUV2RGB(unsigned char* y_in, unsigned char* u_in, unsigned char* v_in, unsigned char* rgb_out, int W, int H);
void InitLookupTable();
```c
在这里插入代码片

(2) yuv2rgb.cpp

#include <iostream>
#include "yuv2rgb.h"
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
//YUV与RGB空间的转换公式:
//R = Y + (R - Y) = Y + 1.14075(V - 128)
//G = Y + (G - Y) = Y - 0.7169(V - 128) - 0.3455(U - 128)
//B = Y + (B - Y) = Y + 1.779(U - 128)
const int maxn = 256;
float yuv11644[maxn], yuv15960[maxn];
float yuv03917[maxn], yuv08127[maxn];
float yuv20170[maxn], yuv00014[maxn];
unsigned char organized(int n)
{
	if (n > 255) n = 255;
	if (n < 0) n = 0;
	return (unsigned char)n;
}
int YUV2RGB(unsigned char* y_in, unsigned char* u_in, unsigned char* v_in, unsigned char* rgb_out, int W, int H)
{
	InitLookupTable();
	unsigned char* Y, * U, * V;
	int size = W * H;
	Y = y_in; U = u_in; V = v_in;
	int pos = 0, rgb_pos = 0;

	for (int i = 0; i < size; ++i)
	{
		int y, u, v, r, g, b;
		int h = i / W, w = i % W, pos;
		if (w & 1) w--; w >>= 1;
		if (h & 1) h--; h >>= 1;
		pos = h * W / 2 + w;
		y = Y[i]; u = U[pos]; v = V[pos];
		r = yuv11644[y] + yuv15960[v];
		g = yuv11644[y] - yuv03917[u] - yuv08127[v];
		b = yuv11644[y] + yuv20170[u] - yuv00014[v];
		rgb_out[rgb_pos++] = organized(b);
		rgb_out[rgb_pos++] = organized(g);
		rgb_out[rgb_pos++] = organized(r);
	}
	return 0;
}
void InitLookupTable()
{
	for (int i = 0; i < 256; ++i)
	{
		yuv00014[i] = 0.0014 * (i - 128);
		yuv03917[i] = 0.3917 * (i - 128);
		yuv08127[i] = 0.8127 * (i - 128);
		yuv11644[i] = 1.1644 * (i - 16);
		yuv15960[i] = 1.5960 * (i - 128);
		yuv20170[i] = 2.0170 * (i - 128);
	}
}

(3) main.cpp

#include <iostream>
#include "yuv2rgb.h"
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

#define u_int8_t	unsigned __int8
#define u_int		unsigned __int32
#define u_int32_t	unsigned __int32

int main(int argc, char* argv[])
{


	//两个文件指针
	FILE* yuvFile = NULL;
	FILE* rgbFile = NULL;

	const char* yuvFileName = "down.yuv";
	const char* rgbFileName = "down.rgb";
	u_int Width = 256;			//定义图像的宽、高变量并赋值
	u_int Height = 256;

	fopen_s(&yuvFile, "down.yuv", "rb");//打开yuv文件
	if (yuvFile == NULL)
	{
		printf("cannot find yuv file\n");
		exit(1);
	}
	else
	{
		printf("The input yuv file is %s\n", yuvFileName);
	}

	fopen_s(&rgbFile, "down.rgb", "wb");//创建rgb文件
	if (rgbFile == NULL)
	{
		printf("cannot find rgb file\n");
		exit(1);
	}
	else
	{
		printf("The output rgb file is %s\n", rgbFileName);
	}

	//四个动态内存区指针	
	unsigned char* yBuffer = NULL;
	unsigned char* uBuffer = NULL;
	unsigned char* vBuffer = NULL;
	unsigned char* rgbBuffer = NULL;

	//分配动态内存	
	yBuffer = (unsigned char*)malloc(sizeof(unsigned char) * Width * Height);
	uBuffer = (unsigned char*)malloc(sizeof(unsigned char) * Width * Height / 4);
	vBuffer = (unsigned char*)malloc(sizeof(unsigned char) * Width * Height / 4);
	rgbBuffer = (unsigned char*)malloc(sizeof(unsigned char) * Width * Height * 3);

	//从三个文件中读出数据 写入动态内存空间yuv
	if (fread(yBuffer, sizeof(unsigned char), Width * Height, yuvFile) &&
		fread(uBuffer, sizeof(unsigned char), Width * Height / 4, yuvFile) &&
		fread(vBuffer, sizeof(unsigned char), Width * Height / 4, yuvFile))
		printf("The file\t%s\twas read\n", rgbFileName);
	else
		printf("The file\t%s\twas not read\n", rgbFileName);


	//调用YUV2RGB函数实现转换
	if ((YUV2RGB(yBuffer, uBuffer, vBuffer, rgbBuffer, Width, Height)) == 0)
		printf("changed successfully");
	else
	{
		printf("fail");
		exit(1);
	}

	//将动态缓冲区数据写入rgb文件
	fwrite(rgbBuffer, sizeof(unsigned char), Width * Height * 3, rgbFile);

	//释放内存,关闭文件
	free(yBuffer);
	free(uBuffer);
	free(vBuffer);
	free(rgbBuffer);

	fclose(rgbFile);
	fclose(yuvFile);
	system("pause");
	return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值