Chinaunix首页 | 论坛 | 博客
  • 博客访问: 215497
  • 博文数量: 74
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 732
  • 用 户 组: 普通用户
  • 注册时间: 2018-06-12 20:01
个人简介

寫写code、调調bug、填填坑,僅此而已。

文章分类

全部博文(74)

文章存档

2019年(25)

2018年(50)

分类: C/C++

2019-05-06 14:56:50

最近研究jpeg图像的编码解码,涉及到DCT即离散余弦变换,参考CSDN上一位calcular博客的文章,进行简易修改。

点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. #define PI 3.14159265354
  5.  
  6. #define DCT_SIZE 8
  7.  
  8. float DCT_Mat[DCT_SIZE][DCT_SIZE];

  9.   float DctMap[DCT_SIZE][DCT_SIZE]=
  10. {
  11.     1,2,3,4,5,6,7,8,
  12.     9,10,11,12,13,14,15,16,
  13.     17,18,19,20,21,22,23,24,
  14.     25,26,27,28,29,30,31,32,
  15.     33,34,35,36,37,38,39,40,
  16.     41,42,43,44,45,56,47,48,
  17.     49,50,51,52,53,54,55,56,
  18.     57,58,59,60,61,62,63,64

  19. };
  20. float DCT_COS[DCT_SIZE][DCT_SIZE] = {
  21.                     0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f,
  22.                     0.49039263f, 0.41573480f, 0.27778509f, 0.09754512f, -0.09754516f, -0.27778518f, -0.41573483f, -0.49039266f,
  23.                     0.46193978f, 0.19134171f, -0.19134176f, -0.46193978f, -0.46193978f, -0.19134156f, 0.19134180f, 0.46193978f,
  24.                     0.41573480f, -0.09754516f, -0.49039266f, -0.27778500f, 0.27778521f, 0.49039263f, 0.09754504f, -0.41573489f,
  25.                     0.35355338f, -0.35355338f, -0.35355332f, 0.35355350f, 0.35355338f, -0.35355362f, -0.35355327f, 0.35355341f,
  26.                     0.27778509f, -0.49039266f, 0.09754521f, 0.41573468f, -0.41573489f, -0.09754511f, 0.49039266f, -0.27778542f,
  27.                     0.19134171f, -0.46193978f, 0.46193978f, -0.19134195f, -0.19134149f, 0.46193966f, -0.46193987f, 0.19134195f,
  28.                     0.09754512f, -0.27778500f, 0.41573468f, -0.49039260f, 0.49039271f, -0.41573480f, 0.27778557f, -0.09754577f
  29.                  };
  30. float IDCT_COS[DCT_SIZE][DCT_SIZE] = {
  31.                     0.35355338f, 0.49039263f, 0.46193978f, 0.41573480f, 0.35355338f, 0.27778509f, 0.19134171f, 0.09754512f,
  32.                     0.35355338f, 0.41573480f, 0.19134171f, -0.09754516f, -0.35355338f, -0.49039266f, -0.46193978f, -0.27778500f,
  33.                     0.35355338f, 0.27778509f, -0.19134176f, -0.49039266f, -0.35355332f, 0.09754521f, 0.46193978f, 0.41573468f,
  34.                     0.35355338f, 0.09754512f, -0.46193978f, -0.27778500f, 0.35355350f, 0.41573468f, -0.19134195f, -0.49039260f,
  35.                     0.35355338f, -0.09754516f, -0.46193978f, 0.27778521f, 0.35355338f, -0.41573489f, -0.19134149f, 0.49039271f,
  36.                     0.35355338f, -0.27778518f, -0.19134156f, 0.49039263f, -0.35355362f, -0.09754511f, 0.46193966f, -0.41573480f,
  37.                     0.35355338f, -0.41573483f, 0.19134180f, 0.09754504f, -0.35355327f, 0.49039266f, -0.46193987f, 0.27778557f,
  38.                     0.35355338f, -0.49039266f, 0.46193978f, -0.41573489f, 0.35355341f, -0.27778542f, 0.19134195f, -0.09754577f
  39.                  };
  40. float DctMapTmp[DCT_SIZE][DCT_SIZE];
  41.  
  42. void InitDctMat()
  43. {
  44.     int i,j,t;
  45.     for(i=0;i<DCT_SIZE;i++) DCT_Mat[0][i]=sqrt(2.0/DCT_SIZE)*sqrt(1.0/2);
  46.     for(i=1;i<DCT_SIZE;i++)
  47.     {
  48.         t=0;
  49.         for(j=0;j<DCT_SIZE;j++)
  50.         {
  51.             DCT_Mat[i][j]=sqrt(2.0/DCT_SIZE)*cos(PI/(2.0*DCT_SIZE)*(i+t));t+=2*i;
  52.         }
  53.     }
  54.     cout<<"=========initDctMat========"<<endl;
  55.     for(int i=0;i<DCT_SIZE;i++)
  56.     {
  57.         for(int j=0;j<DCT_SIZE;j++)
  58.         {
  59.             cout<<DCT_Mat[i][j]<<" ";
  60.         }
  61.         cout<<endl;
  62.     }
  63.     cout<<"=========initDctMat========"<<endl;
  64. }
  65.  
  66. void DCT()
  67. {
  68.     float t=0;
  69.     int i,j,k;
  70.     for(i=0;i<DCT_SIZE;i++){
  71.         for(j=0;j<DCT_SIZE;j++){
  72.             t=0;
  73.             for(k=0;k<DCT_SIZE;k++)
  74.                 //t+=DCT_Mat[i][k]*DctMap[k][j];
  75.                 t+=DCT_COS[i][k]*DctMap[k][j];
  76.             DctMapTmp[i][j]=t;
  77.         }
  78.     }
  79.     for(i=0;i<DCT_SIZE;i++){
  80.         for(j=0;j<DCT_SIZE;j++){
  81.             t=0;
  82.             for(k=0;k<DCT_SIZE;k++)
  83.                 //t+=DctMapTmp[i][k]*DCT_Mat[j][k];
  84.                 t+=DctMapTmp[i][k]*DCT_COS[j][k];
  85.             DctMap[i][j]=t;
  86.         }
  87.     }

  88.     cout<<"=========DCT========"<<endl;
  89.     for(int i=0;i<DCT_SIZE;i++)
  90.     {
  91.         for(int j=0;j<DCT_SIZE;j++)
  92.         {
  93.             cout<<DctMap[i][j]<<" ";
  94.         }
  95.         cout<<endl;
  96.     }
  97.     cout<<"=========DCT========"<<endl;
  98. }
  99.  
  100. void IDCT()
  101. {
  102.     float t=0;
  103.     int i,j,k;
  104.     for(i=0;i<DCT_SIZE;i++){
  105.         for(j=0;j<DCT_SIZE;j++){
  106.             t=0;
  107.             for(k=0;k<DCT_SIZE;k++)
  108.                 //t+=DCT_Mat[k][i]*DctMap[k][j];
  109.                 t+=IDCT_COS[i][k]*DctMap[k][j];
  110.             DctMapTmp[i][j]=t;
  111.         }
  112.     }
  113.     for(i=0;i<DCT_SIZE;i++){
  114.         for(j=0;j<DCT_SIZE;j++){
  115.             t=0;
  116.             for(k=0;k<DCT_SIZE;k++)
  117.                 //t+=DctMapTmp[i][k]*DCT_Mat[k][j]
  118.                 t+=DctMapTmp[i][k]*IDCT_COS[j][k];
  119.             DctMap[i][j]=(int)(t);
  120.         }
  121.     }
  122.      cout<<"=========IDCT========"<<endl;
  123.     for(int i=0;i<DCT_SIZE;i++)
  124.     {
  125.         for(int j=0;j<DCT_SIZE;j++)
  126.         {
  127.             cout<<DctMap[i][j]<<" ";
  128.         }
  129.         cout<<endl;
  130.     }
  131.     cout<<"=========IDCT========"<<endl;
  132. }
  133.  
  134. int main()
  135. {
  136.     //InitDctMat();
  137.     DCT();
  138.     IDCT();
  139.     
  140. }

阅读(1438) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册