简体   繁体   中英

Discrete cosine Transform (DCT) in java

This is the sourcecode DCT .. !!! unknown array of 4x4 DCT will be done by splitting the 4x4 array into several blocks past yyang 2x2 transformation on each block ... !!! I want to ask, how to display an array that has been done, but with size 4x4 transformation, and not the size of 2x2 .. !!! because it is only as a condition 2x2 to be transformed, once transformed restored to its original array size 4x4. please help...!!!

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(" ");
        }

}

}

This is for the sourcecode 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;
}

}

display if the source code above is executed. so this program as a 4x4 array F, then do DCT on F 4x4 4x4 array but by breaking into pieces the size of 2x2. so, 4x4 array will consist of 4 parts 2x2, then each part is done transformations and array F will be f. however, when displaying the array f, which appear only the red stripes !! there may be a mistake, so I please help...! eksekusi

This is an example of an overview of the program that I want : 在此处输入图片说明

Try this. Change a little bit in your code:

In Main class:

Change the testing part of the for loop where you are printing F[][] to i<4 (in place of i<2 ) and j<4 (in place of j<2 ).

In DCT class:

Change F[u][v] = sum; to F[u+row][v+column] = sum; inside the for loops.

Here's a simple function that will print any 2 dimensional matrix:

    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);
    }

So you would call it like this:

printMatrix(F);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM