DCT变换具体概念性质参考博客https://blog.csdn.net/BigDream123/article/details/101426393
这里仅仅描述DCT-2变换的C++实现。
DCT-2正变换的公式如下:
DCT-2反变换公式如下:
#include
#include
#include
#include
using namespace std;#define NUM 8
#define PI 3.1415926int around(double a)
{if(a >= 0){return int(a+0.5);}else{return int(a-0.5);}}
// DCT - Discrete Cosine Transform
void DCT(int data[NUM][NUM])
{int output[NUM][NUM];double alpha,beta;//C(k) C(l)int m=0,n=0,k=0,l=0;for(k = 0;k //Inverse DCT
void IDCT(int data[NUM][NUM])
{int output[NUM][NUM];double alpha,beta;int m&#61;0,n&#61;0,k&#61;0,l&#61;0;for(m &#61; 0;m {int input[NUM][NUM] &#61;{{89, 101, 114, 125, 126, 115, 105, 96},{97, 115, 131, 147, 149, 135, 123, 113},{114, 134, 159, 178, 175, 164, 149, 137},{121, 143, 177, 196, 201, 189, 165, 150},{119, 141, 175, 201, 207, 186, 162, 144},{107, 130, 165, 189, 192, 171, 144, 125},{97, 119, 149, 171, 172, 145, 117, 96},{88, 107, 136, 156, 155, 129, 97, 75}};DCT(input);cout <<"The result of DCT is:\n";for(int i &#61; 0;i }
由于DCT变换的可分离性&#xff0c;可以将二维DCT变换分解为两个一维DCT变换&#xff0c;如下
代码实现如下&#xff1a;
#include
#include
#include
#include
using namespace std;#define NUM 8
#define PI 3.1415926int around(double a)
{if(a >&#61; 0){return int(a&#43;0.5);}else{return int(a-0.5);}}
//两次一维DCT变换
void DCT_1D_1D(int data[NUM][NUM])
{int temp[NUM][NUM],output[NUM][NUM];double alpha,beta;int m&#61;0,n&#61;0,k&#61;0,l&#61;0;//一维DCT水平变换for(m &#61; 0;m //Inverse DCT
void IDCT(int data[NUM][NUM])
{int output[NUM][NUM];double alpha,beta;int m&#61;0,n&#61;0,k&#61;0,l&#61;0;for(m &#61; 0;m {int input[NUM][NUM] &#61;{{89, 101, 114, 125, 126, 115, 105, 96},{97, 115, 131, 147, 149, 135, 123, 113},{114, 134, 159, 178, 175, 164, 149, 137},{121, 143, 177, 196, 201, 189, 165, 150},{119, 141, 175, 201, 207, 186, 162, 144},{107, 130, 165, 189, 192, 171, 144, 125},{97, 119, 149, 171, 172, 145, 117, 96},{88, 107, 136, 156, 155, 129, 97, 75}};DCT_1D_1D(input);cout <<"The result of 1D-DCT and 1D-DCT is:\n";for(int i &#61; 0;i }