试图实现dct 8 * 8矩阵的逆

我已经设法计算了一个8 * 8矩阵的dct,我在进行反向操作时遇到了麻烦。 任何人都可以看看这段代码并告诉我现在正在做什么。 我应该得到与以前完全相同的值,但我得到不同的值。 我正在读取来自csv文件的输入并将其放入anther csv文件中。 它的编程顺序为c

void idct_func(float inMatrix[8][8]){ double idct, Cu, sum, Cv; int i, j, u, v; float idctMatrix[8][8], greyLevel; FILE * fp = fopen("mydata.csv", "r"); FILE * wp = fopen("idct.csv", "w"); fprintf(fp, "\n Inverse DCT"); for (i = 0; i < 8; ++i) { for (j = 0; j < 8; ++j) { sum = 0.0; for (u = 0; u < 8; u++) { for (v = 0; v < 8; v++) { if (u == 0) Cu = 1.0 / sqrt(2.0); else Cu = 1.0; if (v == 0) Cv = 1.0 / sqrt(2.0); else Cv = (1.0); // Level around 0 greyLevel = idctMatrix[u][v]; idct = (greyLevel * cos((2 * i + 1) * u * M_PI / 16.0) * cos((2 * j + 1) * v * M_PI / 16.0)); sum += idct; } } idctMatrix[i][j] = 0.25 * Cu * Cv * sum; fprintf(wp, "\n %f", idctMatrix[i][j]); } fprintf(wp, "\n"); } 

原始矩阵是:

 {255, 255, 255, 255, 255, 255, 255, 255}, {255, 255, 255, 255, 255, 255, 255, 255}, {255, 255, 255, 255, 255, 255, 255, 255}, {255, 255, 255, 255, 255, 255, 255, 255}, {255, 255, 255, 255, 255, 255, 255, 255}, {255, 255, 255, 255, 255, 255, 255, 255}, {255, 255, 255, 255, 255, 255, 255, 255}, {255, 255, 255, 255, 255, 255, 255, 255}}; 

该dct是:

 2040 0 -0 0 0 0 -0 -0 0 0 0 0 -0 0 -0 0 -0 0 -0 0 0 0 0 0 0 -0 -0 -0 0 -0 -0 0 0 0 -0 0 -0 -0 -0 0 0 -0 -0 -0 -0 0 -0 -0 -0 -0 -0 0 0 0 0 -0 -0 0 0 0 -0 0 -0 0 

计算的idct应与原始id相同

您正在尝试使用idctMatrix[][]作为输入和输出来idctMatrix[][]计算IDCT,因此在此过程中正在修改输入。 那是错的。 您需要为输入(或输出)提供单独的二维数组。

还看起来u和v依赖的缩放因子CuCv被施加在u-和v-环之外。 那也是错的。

编辑 :这是代码,如果你不明白的话:

 #include  #include  #ifndef M_PI #define M_PI 3.14159265358979324 #endif void Compute8x8Dct(const double in[8][8], double out[8][8]) { int i, j, u, v; double s; for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) { s = 0; for (u = 0; u < 8; u++) for (v = 0; v < 8; v++) s += in[u][v] * cos((2 * u + 1) * i * M_PI / 16) * cos((2 * v + 1) * j * M_PI / 16) * ((i == 0) ? 1 / sqrt(2) : 1) * ((j == 0) ? 1 / sqrt(2) : 1); out[i][j] = s / 4; } } void Compute8x8Idct(const double in[8][8], double out[8][8]) { int i, j, u, v; double s; for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) { s = 0; for (u = 0; u < 8; u++) for (v = 0; v < 8; v++) s += in[u][v] * cos((2 * i + 1) * u * M_PI / 16) * cos((2 * j + 1) * v * M_PI / 16) * ((u == 0) ? 1 / sqrt(2) : 1.) * ((v == 0) ? 1 / sqrt(2) : 1.); out[i][j] = s / 4; } } void Print8x8(const char* title, const double in[8][8]) { int i, j; printf("%s\n", title); for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) printf("%8.3f ", in[i][j]); printf("\n"); } } int main(void) { double pic1[8][8], dct[8][8], pic2[8][8]; int i, j; for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) #if 01 pic1[i][j] = 255; #else pic1[i][j] = (i ^ j) & 1; #endif Print8x8("pic1:", pic1); Compute8x8Dct(pic1, dct); Print8x8("dct:", dct); Compute8x8Idct(dct, pic2); Print8x8("pic2:", pic2); return 0; } 

这是它的输出:

 pic1: 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 dct: 2040.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 pic2: 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000 255.000