Trouble using PCA...
Page 1 of 1

Author:  matze [ Thu Aug 04, 2011 12:15 am ]
Post subject:  Trouble using PCA...

Hi all,

can someone help me with using pcabuildbasis?
I'm wondering why the following code doesn't reproduce the original matrix "faces":

  //matrix faces: a column is a "face"-image represented in greyscale-pixels (rows=numFaces)

  pcabuildbasis(faces, numPixels, numFaces, info, coeffs, eigenFaces);

  // reproduce faces by scalar-multiplying coeffs with eigenFaces and adding mean:
  for (int face = 0; face<numFaces; ++face) {
    for (int pixel = 0; pixel<numPixels; ++pixel) {
      double reconstruct = 0.0;
      // reconstruct pixel using all basisfunctions
      for (int eigenface = 0; eigenface<numPixels; ++eigenface) {         
        reconstruct += eigenFaces[pixel][eigenface] * coeffs[eigenface];
      reconstruct += means[face];
      matrix[pixel][face] = reconstruct;     
  // Why is matrix!=faces ?

The values stored in matrix are not even in the range of the values stored in faces.
What am I doing wrong?
Thanks for hints!

Author:  Sergey.Bochkanov [ Sat Aug 06, 2011 6:31 pm ]
Post subject:  Re: Trouble using PCA...

Basis vectors built by pcabuildbasis() are orthonormal, i.e. elements of eigenFaces are always in [-1,+1]. These elements describe directions of spread of your dataset, but they do not contain offset/scaling information. For example, if you try to apply pcabuildbasis() to 1-dimensional dataset [1000,1001,1002] then you will get [+1] or [-1] as eigenvector. As you see - no offset information.

And you've misunderstood meaning of coeffs - this array is not array of coefficients. It is array of variances.

If you want to reproduce your matrix (just to make sure that basis was correctly built), your code should look like:

// here I assume that all elements were correctly initialized
pcabuildbasis(faces, numPixels, numFaces, info, coeffs, eigenFaces);
for (int face_to_reconstruct = 0; face_to_reconstruct<numFaces; ++face_to_reconstruct)
    for(pixel = 0; pixel<numPixels; ++pixel)
        reconstructed[pixel] = 0.0;
    for (int eigenface = 0; eigenface<numPixels; ++eigenface)
        // coefficient before eigenface
        double coefficient = 0.0;

        // calculate coefficient
        for (int pixel = 0; pixel<numPixels; ++pixel)
            coefficient += eigenFaces[pixel][eigenface]*faces[face_to_reconstruct];

        // update reconstructed face
        for (int pixel = 0; pixel<numPixels; ++pixel)
            reconstructed[pixel] += eigenFaces[pixel][eigenface]*coefficient;

I may have some errors because I've never actually executed this code, but I hope that idea should be clear.

Author:  matze [ Sat Aug 06, 2011 9:14 pm ]
Post subject:  Re: Trouble using PCA...

I see, I was totally wrong. Now it works, thanks for your support!

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group