fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. const int N = 4;
  6.  
  7. int Q[N][N] = {
  8. {3, 0, 0, 0},
  9. {0, 0, 0, 0},
  10. {0, 0, 0, 0},
  11. {0, 0, 0, 0}
  12. };
  13.  
  14. int QMatrix[N][N] = {
  15. {16, 11, 10, 16},
  16. {12, 12, 14, 19},
  17. {14, 13, 16, 24},
  18. {14, 17, 22, 29}
  19. };
  20.  
  21. double alpha(int x) {
  22. return x == 0 ? 1.0 / sqrt(2.0) : 1.0;
  23. }
  24.  
  25. int main() {
  26. double DCT[N][N], result[N][N] = {0};
  27.  
  28. for (int i = 0; i < N; i++)
  29. for (int j = 0; j < N; j++)
  30. DCT[i][j] = Q[i][j] * QMatrix[i][j];
  31.  
  32. for (int x = 0; x < N; x++) {
  33. for (int y = 0; y < N; y++) {
  34. for (int u = 0; u < N; u++) {
  35. for (int v = 0; v < N; v++) {
  36. result[x][y] += alpha(u) * alpha(v) * DCT[u][v] *
  37. cos((2 * x + 1) * u * M_PI / 8.0) *
  38. cos((2 * y + 1) * v * M_PI / 8.0);
  39. }
  40. }
  41. result[x][y] *= 0.25;
  42. }
  43. }
  44.  
  45. cout << "Відновлена матриця зображення:\n";
  46. for (int i = 0; i < N; i++) {
  47. for (int j = 0; j < N; j++)
  48. printf("%6.2f ", result[i][j]);
  49. cout << endl;
  50. }
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Відновлена матриця зображення:
  6.00   6.00   6.00   6.00 
  6.00   6.00   6.00   6.00 
  6.00   6.00   6.00   6.00 
  6.00   6.00   6.00   6.00