DCT离散余弦变换C版本【转】

来自: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;
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值