来自:http://blog.csdn.net/calcular/article/details/46804829
#include<iostream>
#include<math.h>
using namespace std;
#define PI 3.14159265354
#define DCT_SIZE 8
float DCT_Mat[DCT_SIZE][DCT_SIZE];
float DctMap[DCT_SIZE][DCT_SIZE]=
{
130,130,130,129,134,133,129,130,
130,130,130,129,134,133,130,130,
130,130,130,129,132,132,130,130,
129,130,130,129,130,130,129,129,
127,128,127,129,131,129,131,130,
127,128,127,128,127,128,132,132,
125,126,129,129,127,129,133,132,
127,125,128,128,126,130,131,131
};
//需要DCT处理的图像输入口
/*float DctMap[DCT_SIZE][DCT_SIZE]=
{
0,0,0,0,
0,1,1,0,
0,1,1,0,
0,0,0,0
};*/
float DctMapTmp[DCT_SIZE][DCT_SIZE];
void InitDctMat()
{
int i,j,t;
for(i=0;i<DCT_SIZE;i++) DCT_Mat[0][i]=sqrt(2.0/DCT_SIZE)*sqrt(1.0/2);
for(i=1;i<DCT_SIZE;i++)
{
t=0;
for(j=0;j<DCT_SIZE;j++)
{
DCT_Mat[i][j]=sqrt(2.0/DCT_SIZE)*cos(PI/(2.0*DCT_SIZE)*(i+t));t+=2*i;
}
}
}
void DCT()
{
float t=0;
int i,j,k;
for(i=0;i<DCT_SIZE;i++){
for(j=0;j<DCT_SIZE;j++){
t=0;
for(k=0;k<DCT_SIZE;k++)
t+=DCT_Mat[i][k]*DctMap[k][j];
DctMapTmp[i][j]=t;
}
}
for(i=0;i<DCT_SIZE;i++){
for(j=0;j<DCT_SIZE;j++){
t=0;
for(k=0;k<DCT_SIZE;k++)
t+=DctMapTmp[i][k]*DCT_Mat[j][k];
DctMap[i][j]=t;
}
}
}
void IDCT()
{
float t=0;
int i,j,k;
for(i=0;i<DCT_SIZE;i++){
for(j=0;j<DCT_SIZE;j++){
t=0;
for(k=0;k<DCT_SIZE;k++)
t+=DCT_Mat[k][i]*DctMap[k][j];
DctMapTmp[i][j]=t;
}
}
for(i=0;i<DCT_SIZE;i++){
for(j=0;j<DCT_SIZE;j++){
t=0;
for(k=0;k<DCT_SIZE;k++)
t+=DctMapTmp[i][k]*DCT_Mat[k][j];
DctMap[i][j]=(int)(t);
}
}
}
int main()
{
InitDctMat();
DCT();
for(int i=0;i<DCT_SIZE;i++)
{
for(int j=0;j<DCT_SIZE;j++)
{
cout<<DctMap[i][j]<<" ";
}
cout<<endl;
}
IDCT();
for(int i=0;i<DCT_SIZE;i++)
{
for(int j=0;j<DCT_SIZE;j++)
{
cout<<DctMap[i][j]<<" ";
}
cout<<endl;
}
}