我想在图像上应用DCT,但在这么大的矩阵上做之前,我想在2X2矩阵上应用DCT和IDCT。 以下是我为在2X2矩阵上执行DCT和IDCT而编写的代码。但是在IDCT之后我没有回到原始矩阵。 我哪里出错了?
package dct;
/**
*
* @author jain
*/
public class try4 {
public static void main(String[] args)
{
int n = 2;
double[][] ob = new double[n][n];
double[][] dct = new double[n][n];
double[][] rb = new double[n][n];
double[] c = new double[2];
// initialize co-efficients
c[0] = 1/Math.sqrt(2);
c[1] = 1;
ob[0][0] = 54.0;
ob[0][1] = 35.0;
ob[1][0] = 28.0;
ob[1][1] = 45.0;
for(int u = 0; u < 2;u++)
{
for(int v =0; v < 2;v++)
{
double sum = 0;
for(int j = 0;j < 2; j++)
{
for(int i = 0;i < 2;i++)
{
//sum += Math.cos(((2*i+1)/(2.0*n))*u*Math.PI)*Math.cos(((2*j+1)/(2.0*n))*v*Math.PI)*ob[i][j];
sum += Math.cos(( (2*i + 1) * (u*Math.PI) ) / (2*n) ) * Math.cos(( (2*j + 1) * (v*Math.PI) ) / (2*n) ) * ob[i][j];
}
}
sum = sum * (2/n) * c[u]*c[v];
dct[u][v] = sum;
}
}
System.out.println("The DCT matrix is ");
for(int i= 0; i < 2;i++)
{
for(int j = 0;j < 2; j++)
{
System.out.print(dct[i][j] + "\t");
}
System.out.println();
}
for(int u = 0; u < 2;u++)
{
for(int v =0; v < 2;v++)
{
double sum = 0;
for(int j = 0;j < 2; j++)
{
for(int i = 0;i < 2;i++)
{
//sum +=c[u]*c[v]*dct[u][v] * Math.cos( ((2*i+1)/(2.0*n))) *Math.cos(((2*j+1)/(2.0*n))*v*Math.PI);
sum += c[u]*c[v]*dct[u][v] * Math.cos( ( (2*i + 1) * (u*Math.PI) ) / (2*n) ) * Math.cos(( (2*j + 1) * (v*Math.PI) ) / (2*n) );
}
}
sum = sum * (2/n);
rb[u][v] = sum;
}
}
System.out.println("The retrieved matrix is ");
for(int i= 0; i < 2;i++)
{
for(int j = 0;j < 2; j++)
{
System.out.print(rb[i][j] + "\t");
}
System.out.println();
}
}// main ends
}// class ends