Experiment 1 --- YUV file Convert to RGB file

I. Experimental requirements

WriteYUVConvert toRGBprogram of. Convert the given experimental data toRGBfile. And originalRGBThe file is compared, if there is an error, what is the analysis error comes from. to sum up RGB with YUV Conversion formula and programming implementation of color space conversion and writes an experimental report.

2. Experimental Principle

  1. RGBConvert to YUV formula:

Y=0.299R+0.587G+0.114B

U=-0.1684R-0.3316G-0.1140B

V=0.5R-0.4187G-0.0813B

  1. YUVConvert to RGB formula:

R=Y+1.4075(V-128)

G=Y-0.3455(U-128)-0.7169(V-128)

B=Y+1.779(U-128)

*Since the UV component is set to +128 when calculating the UV component, it is limited to [0,255], so it is necessary to minus the UV 128 when the conversion of YUV is performed to the RGB.

 

Experiment process

  1. ​​​​​​​Main program construction:

​​​​​​​1)Overall process: First, create Height * Width * 5/4 unsigned char space (YUV value is [0,255], so use unsigned character storage, due to the sampling format 4: 2: 0, so Y Y Occupy HEIGHT * WIDTH, UV occupies the area of ​​Height * Width / 4, pays attention to whether height and width are even as an even number and whether space is successfully opened. If it does not satisfy the above two points, exit the program and return 1. Read the original .yuv file, the parameter value inputs the self-built function YUV2RGB to perform YUV conversion to the RGB, and the resulting RGB data reads in the Down_Out.RGB file.

2)Ideological map:

3Key code and analysis:

filename_yuv=argv[1];

filename_rgb=argv[2];

height=atoi(argv[3]);

width=atoi(argv[4]);

The original .rgb file name and image width are read from the original .rgb file name, and image wide.

Set mode:

4) Full code:

 

int main(int argc, char** argv)
{
	int height,width;
	char *filename_yuv,*filename_rgb;
	filename_yuv=argv[1];
	filename_rgb=argv[2];
	height=atoi(argv[3]);
	width=atoi(argv[4]);
	unsigned char *YUV_buffer=NULL,*Y_buffer,*U_buffer,*V_buffer;
	unsigned char *RGB_buffer=NULL;
	unsigned char *YUV_buffer_del,*RGB_buffer_del;
	YUV_buffer=new unsigned char[height*width*3/2];
	YUV_buffer_del=YUV_buffer;
	if((width%2==1)||(height%2==1))
	{
		printf("please input the right height and width/n");
	}
	if(YUV_buffer==NULL)
	{
		printf("can not new unsignde char/n");
		exit(1);
	}
	FILE *fp_yuv=NULL,*fp_rgb=NULL;
	fp_yuv=fopen(filename_yuv,"rb");
	fp_rgb=fopen(filename_rgb,"wb");
	if((fp_yuv==NULL)||(fp_rgb==NULL))
	{
		printf("can not open the file/n");
		exit(1);
	}
	fread(YUV_buffer,sizeof(unsigned char),height*width*3/2,fp_yuv);
	Y_buffer=YUV_buffer;
	U_buffer=YUV_buffer+height*width;
	V_buffer=YUV_buffer+(height*width*5)/4;
	RGB_buffer=new unsigned char[height*width*3];
	RGB_buffer_del=RGB_buffer;
	if(RGB_buffer==NULL)
	{
		printf("can not new unsigned char/n");
		exit(1);
	}
	YUV2RGB(height,width,Y_buffer,U_buffer,V_buffer,RGB_buffer);
	fwrite(RGB_buffer,sizeof(unsigned char),height*width*3,fp_rgb);
	fclose(fp_yuv);
	fclose(fp_rgb);
	delete[] YUV_buffer_del;
	delete[] RGB_buffer_del;
	return 0;
}

2. Self-editing function YUV2RGB:

1) Process: First separate the UV of Height * Width / 4 size into the HEIGHT * WIDTH size space, which is convenient to follow-up, and then calculate its RGB value according to YUV of each pixel.

2) Key code and analysis:

& 1. Extension UV to Height * Width Size:

            *pu_up1=*U__buffer;
			*(pu_up1+1)=*U__buffer;
			*pu_up2=*U__buffer;
			*(pu_up2+1)=*U__buffer;
			pu_up1+=2;
			pu_up2+=2;

			*pv_up1=*V__buffer;
			*(pv_up1+1)=*V__buffer;
			*pv_up2=*V__buffer;
			*(pv_up2+1)=*V__buffer;
			pv_up1+=2;
			pv_up2+=2;

Each time you operate four adjacent pixels, where PU_UP1 points to the position of the first row of the first row in four pixels, PU_UP2 points to the position of the second row. After each operation, PU_UP1 and PU_UP2 need to be moved two times respectively. The operation of V is the same.

&2.

if(i!=(height/2))
		{
			pu_up1+=width;
			pu_up2+=width;
			pv_up1+=width;
			pv_up2+=width;
		}

In addition to the rest of the last field, the rest of the last field will be moved to the next row, in addition to the position of the last field, PU_UP2, PV_UP1, PV_UP2.

&3.

if((y[*Y__buffer]+v14075[*v_buffer])<0)
			*r_buffer=(unsigned char)0;
		else
			if((y[*Y__buffer]+v14075[*v_buffer])>255)
				*r_buffer=(unsigned char)255;
			else *r_buffer=(unsigned char)(y[*Y__buffer]+v14075[*v_buffer]);

Judging the RGB value of each pixel, if it is less than zero, then take 255 more than 255, prevent the unsigned char value overflow

&4.

    float v14075[256],u03455[256],v07169[256],u1779[256],y[256];
	for(int i=0;i<256;i++) v14075[i]=(i-128)*1.4075;
	for(int i=0;i<256;i++) u03455[i]=(i-128)*0.3455;
	for(int i=0;i<256;i++) v07169[i]=(i-128)*0.7169;
	for(int i=0;i<256;i++) u1779[i]=(i-128)*1.779;
	for(int i=0;i<256;i++) y[i]=i;

With spatial exchange efficiency, the introduction of the 1.4075 * (V-128) equation at V-Take [0,255] all values ​​when V-Take [0,255], and only according to V value according to V, no need to calculate.

3) Full code:

#include<stdio.h>
#include<stdlib.h>
//static float v14075[256],u03455[256],v07169[256],u1779[256];
//void YUVTable()
//{
//	 for(int i=0;i<256;i++) v14075[i]=i*1.4075;
//	 for(int i=0;i<256;i++) u03455[i]=i*0.3455;
//	 for(int i=0;i<256;i++) v07169[i]=i*0.7169;
//	 for(int i=0;i<256;i++) u1779[i]=i*1.779;
//}
		
int YUV2RGB(int height,int width,unsigned char *Y__buffer,unsigned char *U__buffer,unsigned char *V__buffer,unsigned char *RGB__buffer)
{
	float v14075[256],u03455[256],v07169[256],u1779[256],y[256];
	for(int i=0;i<256;i++) v14075[i]=(i-128)*1.4075;
	for(int i=0;i<256;i++) u03455[i]=(i-128)*0.3455;
	for(int i=0;i<256;i++) v07169[i]=(i-128)*0.7169;
	for(int i=0;i<256;i++) u1779[i]=(i-128)*1.779;
	for(int i=0;i<256;i++) y[i]=i;
	unsigned char *u_buffer,*v_buffer;
	unsigned char *pu_up1,*pu_up2,*pv_up1,*pv_up2;
	unsigned char *b_buffer,*g_buffer,*r_buffer;
	unsigned char *u_buffer_del,*v_buffer_del;
	u_buffer=new unsigned char[height*width];
	v_buffer=new unsigned char[height*width];
	u_buffer_del=u_buffer;
	v_buffer_del=v_buffer;
	pu_up1=u_buffer;
	pu_up2=u_buffer+width;
	pv_up1=v_buffer;
	pv_up2=v_buffer+width;
	for(int i=0;i<(height/2);i++)
	{
		for(int j=0;j<(width/2);j++)
		{
			*pu_up1=*U__buffer;
			*(pu_up1+1)=*U__buffer;
			*pu_up2=*U__buffer;
			*(pu_up2+1)=*U__buffer;
			pu_up1+=2;
			pu_up2+=2;

			*pv_up1=*V__buffer;
			*(pv_up1+1)=*V__buffer;
			*pv_up2=*V__buffer;
			*(pv_up2+1)=*V__buffer;
			pv_up1+=2;
			pv_up2+=2;

			U__buffer++;
			V__buffer++;

		}
		if(i!=(height/2))
		{
			pu_up1+=width;
			pu_up2+=width;
			pv_up1+=width;
			pv_up2+=width;
		}
	}
	b_buffer=RGB__buffer;
	g_buffer=RGB__buffer+1;
	r_buffer=RGB__buffer+2;
	For (int i = 0; i <(height * width); i ++) // calculate RGB
	{
		/**r_buffer=(unsigned char)(y[*Y__buffer]+v14075[*v_buffer]);
		*g_buffer=(unsigned char)(y[*Y__buffer]-u03455[*u_buffer]-v07169[*v_buffer]);
		*b_buffer=(unsigned char)(y[*Y__buffer]+u1779[*u_buffer]);*/
		if((y[*Y__buffer]+v14075[*v_buffer])<0)
			*r_buffer=(unsigned char)0;
		else
			if((y[*Y__buffer]+v14075[*v_buffer])>255)
				*r_buffer=(unsigned char)255;
			else *r_buffer=(unsigned char)(y[*Y__buffer]+v14075[*v_buffer]);
		
		if((y[*Y__buffer]-u03455[*u_buffer]-v07169[*v_buffer])<0)
			*g_buffer=(unsigned char)0;
		else
			if((y[*Y__buffer]-u03455[*u_buffer]-v07169[*v_buffer])>255)
				*g_buffer=(unsigned char)255;
			else *g_buffer=(unsigned char)(y[*Y__buffer]-u03455[*u_buffer]-v07169[*v_buffer]);

		if((y[*Y__buffer]+u1779[*u_buffer])<0)
			*b_buffer=(unsigned char)0;
		else
			if((y[*Y__buffer]+u1779[*u_buffer])>255)
				*b_buffer=(unsigned char)255;
			else *b_buffer=(unsigned char)(y[*Y__buffer]+u1779[*u_buffer]);
		b_buffer+=3;
		g_buffer+=3;
		r_buffer+=3;
		Y__buffer++;
		u_buffer++;
		v_buffer++;
	}
	delete[] u_buffer_del;
	delete[] v_buffer_del;
	return 0;
}

Four. Operation results

 

Intelligent Recommendation

[Experiment 1] Use C ++ to achieve YUV and RGB color space conversion

Article catalog First, experimental requirements Second, RGB2YUV experiment 1, experimental principle 2, code debugging: Resolve mistakes Look up 3, experimental results Third, YUV2RGB experiment 1, e...

RGB file with the probability statistics and entropy of each component of YUV file

Tip: After the article is written, the directory can be automatically generated, how to generate the help documentation that can refer to the right. Article catalog First, the experimental purpose Sec...

C language implementation. YUV file conversion to .RGB file

Article catalog First, the experimental purpose Second, the experiment principle 1. YUV and RGB space mutual conversion 2. Code level allocation and digital expression 3. Chroma format Third, the expe...

yuv and rgb data stream written jpeg file template

If YUV turn bluish pictures, it proved to be nv12, nv12 and nv21 data generated by the picture upside down and backwards rgb bgr with the same effect. YUV file generated picture RGB: generating image ...

Statistical RGB and YUV image file probability distribution, and seek entropy

1 experimental purpose Give two picturesdown.rgbwithdown.yuvThe resolution is 256 * 256, the color sampling format 4: 2: 0, the probability distribution of each classification of RGB and YUV image fil...

More Recommendation

The probability distribution of YUV and RGB file and calculation of entropy values

I. Objective: Analyze the probability distribution of the three channels for the Down.RGB and Down.yuv files, and calculate their respective entropy. (Programming implementation) II. Principle: The re...

YUV data format introduction to RGB conversion, image file packaging

Usually we represent a color with RGB. The data displayed by the LCD in the computer system is RGB to represent the color of each pixel. In our lives, there are two kinds of black and white TVs and co...

Experiment 2 BMP sequence transfer YUV file

Basic principle Composition of BMP files BMP (full name Bitmap) is a standard image file format in the Windows operating system, which can be divided into two categories: device-related bitmaps (DDBs)...

Experiment 2 | BMP sequence transfer YUV file

First, experimental requirements Generate multiple BMP files from the image processing software, contain at least 5 different screens, with class, student, named logo. Basic requirements for 24bit BMP...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top