I am implementing DCT transform using this formula:
But the results are incorrect. For some 8 by 8 matrix,
0 0 0 0 0 0 0 0
210 210 210 210 210 210 210 210
255 255 255 255 255 255 255 255
210 210 210 210 210 210 210 210
0 0 0 0 0 0 0 0
210 210 210 210 210 210 210 210
255 255 255 255 255 255 255 255
210 210 210 210 210 210 210 210
The results I got after passing the data to dct transform function are:
1350.000000 0.000000 -0.000000 0.000000 0.000000 0.000000 -0.000000 -0.000000
-250.897627 -0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000
-0.000000 0.000000 0.000000 -0.000000 0.000000 -0.000000 0.000000 -0.000000
-461.931139 -0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000
-510.000000 0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000
156.770200 0.000000 -0.000000 0.000000 0.000000 0.000000 -0.000000 -0.000000
-0.000000 -0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000 -0.000000
-260.946562 -0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000
(Only 1st column has non-zero values)
The problem is that I was told that the correct results should be only non-zero values at upper left corner of the matrix. And I am not sure where might be wrong in my code. Can anyone help me? Thanks.
Here is my DCT code:
static double C(int val){
if(val == 0)
return 1.0 / sqrt(2.0);
else
return 1.0;
}
void dctTransform(int matrix[8][8], double dctMatrix[8][8]){
int u, v, x, y;
double temp;
for(u=0; u<8; u++)
for(v=0; v<8; v++){
temp = 0;
dctMatrix[u][v] = 0;
for(x=0;x<8;x++){
for(y=0;y<8;y++){
temp += matrix[y][x]*cos(((2*x+1)*u*M_PI) / 16)*cos(((2*y+1)*v*M_PI) / 16);
}
}
dctMatrix[u][v] = C(u) * C(v) * 0.25 * temp;
}
}
Aren't you supposed to first change the input values from 0..255 to -128..127? Haven't you also switched
x
andy
inmatrix[y][x]
?If I subtract 128 from the input values to center them around zero, and then switch x and y, your code at least gives me the correct values according to the example in Wikipedia.