【问题标题】:Discrete cosine Transform (DCT) in javajava中的离散余弦变换(DCT)
【发布时间】:2015-02-21 23:25:05
【问题描述】:

这是源代码 DCT .. !!! 4x4 DCT 的未知数组将通过将 4x4 数组拆分为几个块来完成,每个块上的 yyang 2x2 变换...!!!我想问一下,如何显示一个已经完成的数组,但是大小是 4x4 的变换,而不是 2x2 的大小 .. !!!因为它只是作为条件 2x2 进行变换,一旦变换就恢复到原来的 4x4 数组大小。请帮忙...!!!

public class Main {
private static final int N = 4;
private static double[][] f = new double[4][4];
private static Random generator = new Random();

public static void main(String[] args) {
    // Generate random integers between 0 and 255
    int value;
    for (int x=0;x<N;x++) 
    {
        for (int y=0;y<N;y++) 
        {
          value = generator.nextInt(255);
          f[x][y] = value;
          System.out.print(f[x][y]+" ");
        }
         System.out.println();
    }




     DCT dctApplied = new DCT();
    double[][] F = dctApplied.applyDCT(f);
    System.out.println("From f to F");
    System.out.println("-----------");

        for (int i=0;i<2;i++)
        {
          for (int j=0;j<2;j++) 
          {

         try {
             System.out.print(F[i][j]+" ");
             } 
             catch (Exception e)
             {
                System.out.println(e);
             }

         }
             System.out.println(" ");
        }

}

}

这是源代码 DCT

public class DCT {
private static final int N = 2;
private static final int M = 2;

private double[] c = new double[N];

public DCT() 
{
      this.initializeCoefficients();
}

private void initializeCoefficients() 
{
    for (int i=1;i<N;i++) 
    {
        c[i]=1;
    }
    c[0]=1/Math.sqrt(2.0);
}

public double[][] applyDCT(double[][] f)
    {
     double[][] F = new double[4][4];  

    for (int row = 0; row < (f.length); row += 2) {
    for (int column = 0; column < f[0].length; column+=2) {
     for (int u=0;u<N;u++) 
     {
        for (int v=0;v<N;v++) 
         {
           double sum = 0.0;
           for (int i=0;i<N;i++)
           {
             for (int j=0;j<N;j++) 
             {
              f[i][j]=f[row+i][column+j];
               sum+=Math.cos(((2*i+1)/(2.0*N))*u*Math.PI)*Math.cos(((2*j+1)/(2.0*N))*v*Math.PI)*f[i][j];
             }
           }
               sum*=((2*c[u]*c[v])/Math.sqrt(M*N));
               F[u][v]=sum;
        }
    }
    }
     }
    return F;
}

}

如果上面的源代码被执行,则显示。 所以这个程序是一个 4x4 数组 F,然后在 F 4x4 4x4 数组上执行 DCT,但要分成 2x2 大小的块。因此,4x4 数组将由 4 个 2x2 部分组成,然后每个部分都完成转换,数组 F 将是 f。但是,当显示数组 f 时,只出现红色条纹!可能有错误,所以我请帮助......!

这是我想要的程序概述的示例:

【问题讨论】:

  • 所以你想打印从函数applyDCT()返回的数组?要显示矩阵,请在行中使用\t 作为制表符,在行尾使用\n 作为新行。
  • 在DCT过程中,4x4数组在DCT变换中被分割成2x2块……!!!但我问如何显示一个转换回 4x4 数组的数组
  • 你能举个例子吗?
  • 以图片为例
  • 所以这就是我的理解:你在applyDCT 函数中初始化F[][] 作为4*4 数组但你的uv 总是从0 to 2F[u][v] = sum;将始终更新F[][] 的块 1 部分。所以我认为你应该把它改成F[u+row][v+column] = sum;。然后按照上面的建议正常打印F[][]

标签: java arrays dct


【解决方案1】:

试试这个。稍微修改一下你的代码:

在主类中:

将要打印F[][]for 循环的测试部分更改为i&lt;4(代替i&lt;2)和j&lt;4(代替j&lt;2)。

在 DCT 类中:

for 循环内将F[u][v] = sum; 更改为F[u+row][v+column] = sum;

【讨论】:

  • 我刚加入这里,所以我不知道如何将你的答案标记为答案
  • 查看答案左侧的勾号。单击与答案对应的勾号。选中后会变成绿色。
【解决方案2】:

这是一个可以打印任何二维矩阵的简单函数:

    public void printMatrix(double[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        String s = "";

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                s += matrix[row][col] + "\t";
            }

            s += "\n";
        }

        System.out.println(s);
    }

所以你可以这样称呼它:

printMatrix(F);

【讨论】:

  • 为什么投反对票? OP问“我想问,如何显示一个已经完成的数组,但大小为4x4转换,而不是2x2的大小”,并且接受的答案仍然依赖于数字常量(int 4),虽然正确,不如我提供的答案一般。
猜你喜欢
  • 2012-01-08
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多