javamathstatisticslinear-algebrajama

Correlation matrix for eigenfaces - Java


I am following the tutorial at http://jeremykun.com/2011/07/27/eigenfaces/. I am trying to implement this solution in Java using the Jama Linear Algebra package.

I am stuck on calculating the covariance matrix. I calculated all the differenceVectors and stored them each in a 'Matrix'. However, I don't see how to turn these into a covariance matrix.

How do I best go about doing this in Java?


Solution

  • You may do something like this (to deal with the matrix I am importing jama). Actually eigenfaces are implemented below, because there was a problem with this function for java.

    private static void evaluateEigenface(int M,int N,Matrix x,double[] average,double[] eigenvalues,Matrix eigenfaces){
    
            // x is (widthProcessedImage*heightProcessedImage)X(numberProcessedImages);
    
            Matrix w=new Matrix(M,N,0.0);
    
            for(int i=0;i<M;i++){
                average[i]=0;
                for(int j=0;j<N;j++){
                    average[i]=average[i]+x.get(i,j);
                }
                average[i]=average[i]/((double)N);
                //System.out.println(average[i]);
            }
    
    
            for(int i=0;i<M;i++){
                for(int j=0;j<N;j++){
                    w.set(i, j, x.get(i,j)-average[i]);
                }
            }
    
            Matrix auxMat=w.transpose().times(w); // =w'*w
    
            SingularValueDecomposition SVD =  new SingularValueDecomposition(auxMat);
            double[] mu = SVD.getSingularValues(); // Eigenvalues of w'w
            Matrix d=SVD.getU(); // LeftSingularVectors of w'w => Each column is an eigenvector
            Matrix e=w.times(d); // Eigenvector of ww'
    
            for(int i=0;i<N;i++)eigenvalues[i]=mu[i];
    
            double theNorm;
            double[] auxArray=new double[M];
            for(int i=0;i<N;i++){
                for(int j=0;j<M;j++)auxArray[j]=e.get(j,i);
                theNorm=norma2(M,auxArray);
                for(int j=0;j<M;j++)eigenfaces.set(j,i, e.get(j, i)/theNorm); // eigenfaces are the normalized eigenvectors of ww'
            }
    
        }