⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dct.cpp

📁 算是图像压缩里边比较重要的一块吧
💻 CPP
字号:
#include <iostream.h>
#include <math.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define pi 3.1415926
#define ROW 400 //定义行
#define COL 336  //定义列
#define MAX ROW*COL
#define no_blocks ROW/8 * COL/8 //定义有几个8*8的块
double matrix_origin[8][8];
int matrix_aftdct[8][8];

//-----------------------------------------------------------------------------
int  ref_Y[8][8]={{16,11,10,16,24,40,51,61},
			     {12,12,14,19,26,58,60,55},
			     {14,13,16,24,40,57,69,56},
			     {14,17,22,29,51,87,80,62},
			     {18,22,37,56,68,109,103,77},
			     {24,35,55,64,81,104,113,92},
			     {49,64,78,87,103,121,120,101},
			     {72,92,95,98,112,100,103,99}};
int  ref_UV[8][8]={{17,18,24,47,99,99,99,99},
			     {18,21,26,66,99,99,99,99},
			     {24,26,56,99,99,99,99,99},
			     {47,66,99,99,99,99,99,99},
			     {99,99,99,99,99,99,99,99},
			     {99,99,99,99,99,99,99,99},
			     {99,99,99,99,99,99,99,99},
			     {99,99,99,99,99,99,99,99}};
//------------------------------------------------------------
int R[ROW][COL],G[ROW][COL],B[ROW][COL];
int i_R[ROW][COL],i_G[ROW][COL],i_B[ROW][COL];
double Y[ROW][COL],U[ROW][COL],V[ROW][COL],t[ROW][COL];
double aft_V[ROW][COL],final_V[ROW][COL];
double temp[8][8];
//---------------------------------
FILE *inr,*ing,*inb;
//----------------------------------
void READ_RGB()
{
	int i,j;
	cout<<"READ RGB begin..."<<endl;
//-------------------------------------------------------------------------
	inr=fopen("R.txt","r");
	if(inr == NULL)
	{
		cout<<"R_File open error";
	    exit(0);
    }
	for(i = 0;i < ROW;i++)
		for(j = 0;j < COL;j++)
		{
			fscanf(inr,"%d",&R[i][j]);
		}
	fclose(inr);
//--------------------------------------------------------------------------
	ing=fopen("G.txt","r");
	if(ing==NULL)
	{
		cout<<"G_File open error";
	    exit(0);
    }
	for(i = 0;i < ROW;i++)
		for(j = 0;j < COL;j++)
		{
			fscanf(ing,"%d",&G[i][j]);
		}
	fclose(ing);
//---------------------------------------------------------------------------
	inb=fopen("B.txt","r");
	if(inb==NULL)
	{
		cout<<"B_File open error";
	    exit(0);
    }
	for(i = 0;i < ROW;i++)
		for(j = 0;j < COL;j++)
		{
			fscanf(inb,"%d",&B[i][j]);
		}
	fclose(inb);
	cout<<endl;
	cout<<"RGB has been read."<<endl;
}
//---------------------------------------------------------------------------
void RGB_YUV()
{
	for(int i = 0;i < ROW;i++)
		for(int j = 0;j < COL;j++)
		{
			Y[i][j] = 0.299 * R[i][j] + 0.587 * G[i][j] + 0.114 * B[i][j] ;
			U[i][j] = (-0.299) * R[i][j] + (-0.587) * G[i][j] + 0.886 * B[i][j];
			V[i][j] = 0.701 * R[i][j] + (-0.587) * G[i][j] + (-0.114) * B[i][j];
		}
//	cout<<Y[2][2]<<"  ";
}
//----------------------------------------------------------------
double C(double e)
{
	if(e == 0)
		return sqrt(2)/2;
	else
		return 1;
}
//-----------------------------------
double DCT(double u,double v)
{
	double temp = 0,c1,c2;
	int i,j;
	for(i = 0;i < 8;i++)
		for(j = 0;j < 8;j++)
		{
			c1 = (2*i+1)*u*pi/16;
			c2 = (2*j+1)*v*pi/16;
			temp += matrix_origin[i][j] * cos(c1) * cos(c2);
		}
		temp *= C(u);
		temp *= C(v);
		temp /= 4;
	return temp;
}
//-------------------------------------------------------
double IDCT(int i,int j)
{
	double temp = 0,c1,c2;
	int u,v;
	for(u = 0;u < 8;u++)
		for(v = 0;v < 8;v++)
		{
			c1 = (2*i+1)*u*pi/16;
			c2 = (2*j+1)*v*pi/16;
			temp += (C(u)*C(v)/4) * matrix_aftdct[u][v] * cos(c1) * cos(c2);
		}
	return temp;
}
//----------------------------------------
void quan()
{
	for(int i = 0;i < 8;i++)
	{
		cout<<endl;
		for(int j = 0;j < 8;j++)
		{
			temp[i][j] = matrix_aftdct[i][j] / ref_UV[i][j];
			(int)temp[i][j];
			cout<<temp[i][j]<<"  ";
		}
	}
}
//------------------------------------------
void Iquan()
{
	for(int i = 0;i < 8;i++)
	{
		cout<<endl;
		for(int j = 0;j < 8;j++)
		{
			matrix_aftdct[i][j] = temp[i][j] * ref_UV[i][j];
			cout<<matrix_aftdct[i][j]<<"  ";
		}
	}
}
//---------------------------------------------
void compress()
{
	for(int i = 0;i < ROW/8; i+=1)
		for(int j = 0;j < COL/8; j+=1)
		{
			for(int p = 0;p < 8;p++)
				for(int q = 0;q < 8;q++)
					matrix_origin[p][q] = V[i*8+p][j*8+q];
				///////////////////////////////////////
			cout<<"origin-->";
			cout<<"-------------------------------------------------------";
			for(int g = 0;g < 8;g++)
			{
				cout<<endl;
				for(int h = 0;h < 8;h++)
						cout<<matrix_origin[g][h]<<" ";
			}
	//		getchar();
			cout<<endl;
			////////////////////////////////////////
			cout<<"DCT-->";
			cout<<"-------------------------------------------------------";
			double result;
			for(int u = 0;u < 8;u++)
			{
				cout<<endl;
				for(int v = 0;v < 8;v++)
				{
					result = (int)DCT(u,v);
					matrix_aftdct[u][v] = result;
					cout<<result<<"   ";
				}
			}
		//	getchar();
			quan();
			for(p = 0;p < 8;p++)
				for(int q = 0;q < 8;q++)
					aft_V[i*8+p][j*8+q] = temp[p][q];
		}
}
void decompress()
{
	double result;
	for(int i = 0;i < ROW/8; i+=1)
		for(int j = 0;j < COL/8; j+=1)
		{
			for(int p = 0;p < 8;p++)
				for(int q = 0;q < 8;q++)
					temp[p][q] = aft_V[i*8+p][j*8+q];
			cout<<"IQUAN-->";
			cout<<"-------------------------------------------------------";
			Iquan();
		//	getchar();
			cout<<"IDCT-->";
			cout<<"-------------------------------------------------------";
			for(int u = 0;u < 8;u++)
			{
				cout<<endl;
				for(int v = 0;v < 8;v++)
				{
					result = (int)IDCT(u,v);
					temp[u][v] = result;
					cout<<result<<"   ";
				}
			}
		//	getchar();
			for(p = 0;p < 8;p++)
				for(int q = 0;q < 8;q++)
			final_V[i*8+p][j*8+q] = temp[p][q];
		}
}
/*void YUV_RGB()
{
	for (int i = 0; i < ROW ; i ++)
		for (int j = 0; j < COL ; j ++)
		{
			i_R[i][j] = (int)(final_Y[i][j]+ V[i][j]);
			i_G[i][j] = (int)(final_Y[i][j]-(0.299*V[i][j]+0.114*U[i][j])/0.587);
			i_B[i][j] = (int)(final_Y[i][j]+ U[i][j]);
		}
		//	cout<<i_G[3][2];
}
//-----------------------------------------------------
void OUTPUT_RGB()
{
	FILE *p;
	p=fopen("NR.txt","w");
	if(p==NULL)
	{
		cout<<"R_File open error";
	    exit(0);
    }
	for(int i = 0;i < ROW;i++)
		for(int j = 0;j < COL;j++)
		{
			fprintf(p," %d",i_R[i][j]);
		}
	fclose(p);
	//---------------------
	p=fopen("NG.txt","w");
	if(p==NULL)
	{
		cout<<"G_File open error";
	    exit(0);
    }
	for(i = 0;i < ROW;i++)
		for(int j = 0;j < COL;j++)
		{
			fprintf(p," %d",i_G[i][j]);
		}
	fclose(p);
	//-----------------------
	p=fopen("NB.txt","w");
	if(p==NULL)
	{
		cout<<"B_File open error";
	    exit(0);
    }
	for(i = 0;i < ROW;i++)
		for(int j = 0;j < COL;j++)
		{
			fprintf(p," %d",i_B[i][j]);
		}
	fclose(p);	
}*/
void OUTPUT()
{
	FILE*p;
	p=fopen("NV.txt","w");
	if(p==NULL)
	{
		cout<<"NV_File open error";
	    exit(0);
    }
	for(int i = 0;i < ROW;i++)
		for(int j = 0;j < COL;j++)
		{
		//	cout<<final_V[i][j];
			fprintf(p," %d",(int)final_V[i][j]);
		}
	fprintf(p,"\n");
	fclose(p);

}
void main()
{
	READ_RGB();
	RGB_YUV();
	cout<<"------------"<<V[0][0];
	compress();
	decompress();
	OUTPUT();
//	YUV_RGB();
//	OUTPUT_RGB();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -