Created
          April 17, 2015 09:02 
        
      - 
      
- 
        Save zhiyue/bbb98e9e2a921a793df8 to your computer and use it in GitHub Desktop. 
    DCT算法java 实现 。此版本的效率比较低
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class DCT2 { | |
| private static final int N = 130; | |
| private double[] c = new double[N]; | |
| public DCT2() { | |
| 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[N][N]; | |
| 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++) { | |
| 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(N*N); | |
| F[u][v]=sum; | |
| } | |
| } | |
| return F; | |
| } | |
| public double[][] applyDCT(int[][] f){ | |
| double[][] fd = new double[f.length][f[0].length]; | |
| for (int i = 0; i < f.length; i++) { | |
| for (int j = 0; j < f[0].length; j++) { | |
| fd[i][j] = f[i][j]; | |
| } | |
| } | |
| return applyDCT(fd); | |
| } | |
| public double[][] applyIDCT(double[][] F) { | |
| double[][] f = new double[N][N]; | |
| for (int i=0;i<N;i++) { | |
| for (int j=0;j<N;j++) { | |
| double sum = 0.0; | |
| for (int u=0;u<N;u++) { | |
| for (int v=0;v<N;v++) { | |
| sum += (2 * c[u]*c[v])/Math.sqrt(N*N)*Math.cos(((2*i+1)/(2.0*N))*u*Math.PI)*Math.cos(((2*j+1)/(2.0*N))*v*Math.PI)*F[u][v]; | |
| } | |
| } | |
| f[i][j]=Math.round(sum); | |
| } | |
| } | |
| return f; | |
| } | |
| public String getFingerString(double[][] f,int dimension){ | |
| StringBuilder hash = new StringBuilder(); | |
| /* | |
| * 计算DCT的平均值 | |
| */ | |
| double total = 0; | |
| for (int x = 0; x < dimension; x++) { | |
| for (int y = 0; y < dimension; y++) { | |
| total += f[x][y]; | |
| } | |
| } | |
| double avg = total / (double) ((dimension * dimension)); | |
| for (int x = 0; x < dimension; x++) { | |
| for (int y = 0; y < dimension; y++) { | |
| hash.append(f[x][y] > avg? "1" : "0"); | |
| } | |
| } | |
| return hash.toString(); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment