![](https://secure.gravatar.com/avatar/559e84125b0b3f9282adfa291cd5b019.jpg?s=120&d=mm&r=g)
hi guys I have a set of face images with which i want to do face recognition using Petland's PCA method.I gathered these steps from their docs 1.represent matrix of face images data 2.find the adjusted matrix by substracting the mean face 3.calculate covariance matrix (cov=A* A_transpose) where A is from step2 4.find eigenvectors and select those with highest eigenvalues 5.calculate facespace=eigenvectors*A when it comes to implementation i have doubts as to how i should represent the matrix of face images? using PIL image.getdata() i can make an array of each greyscale image. Should the matrix be like each row contains an array representing an image? That will make a matrix with rows=numimages and columns=numpixels cavariancematrix =A *A_transpose will create a square matrix of shape(numimages,numimages) Using numpy.linalg.eigh(covariancematrix) will give eigenvectors of same shape as the covariance matrix. I would like to know if this is the correct way to do this..I have no big expertise in linear algebra so i would be grateful if someone can confirm the right way of doing this RoyG
![](https://secure.gravatar.com/avatar/742736902afc4e0926b96932ada8cbc1.jpg?s=120&d=mm&r=g)
RoyG, The timing of your question couldn't be better, I just did an blog post on this (I also plugged scipy and the EPD): http://www.datawrangling.com/python-montage-code-for-displaying-arrays.html The code basically replicates the matlab montage() function and approach to handling grayscale images using matplotlib. -Pete On Fri, Feb 29, 2008 at 2:15 PM, devnew@gmail.com <devnew@gmail.com> wrote:
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
![](https://secure.gravatar.com/avatar/742736902afc4e0926b96932ada8cbc1.jpg?s=120&d=mm&r=g)
Here is the page I referenced for the octave version ... it includes examples very similar to what you want. I will be posting a very similar example in Python later this month. I don't have any Python code on hand for the Petland paper, but I think matlab example should be easy to translate to scipy/matplotlib using the montage function: load faces.mat %Form covariance matrix C=cov(faces'); %build eigenvectors and eigenvalues [E,D] = eig(C); %sort based on eigenvalue [B,index] = sortrows(D'); E2=E'; E2(index,:)'; eigensorted=E2(index,:)'; %show eigenfaces clear Z; for i=1:length(eigensorted) Z(:,:,1,i)=reshape(eigensorted(:,i)-1.5*min(min(min(eigensorted))), 19,19); end montage(Z) %show top 16 eigenfaces clear Z; for i=1:16 Z(:,:,1,i)=reshape(eigensorted(:,i)-min(min(min(eigensorted))), 19,19); end montage(Z) On Fri, Feb 29, 2008 at 2:50 PM, Peter Skomoroch <peter.skomoroch@gmail.com> wrote:
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
![](https://secure.gravatar.com/avatar/742736902afc4e0926b96932ada8cbc1.jpg?s=120&d=mm&r=g)
Forgot the url: http://www.cis.hut.fi/Opinnot/T-61.2010/harjoitustyo_en07.shtml On Fri, Feb 29, 2008 at 2:56 PM, Peter Skomoroch <peter.skomoroch@gmail.com> wrote:
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
![](https://secure.gravatar.com/avatar/559e84125b0b3f9282adfa291cd5b019.jpg?s=120&d=mm&r=g)
On Mar 1, 12:57 am, "Peter Skomoroch" wrote: I think
hi Peter, nice code..ran the examples.. however couldn't follow the matlab code since i have no exposure to matlab..was using numpy etc for calcs could you confirm the layout for the face images data? i assumed that the initial face matrix should be faces=a numpy matrix with N rows ie N=numofimages row1=image1pixels as a sequence row2=image2pixels as a sequence ... rowN=imageNpixels as a sequence and covariancematrix=faces*faces_transpose is this the right way? thanks
![](https://secure.gravatar.com/avatar/742736902afc4e0926b96932ada8cbc1.jpg?s=120&d=mm&r=g)
I think that is correct... Here is what the final result should look like: http://www.datawrangling.com/media/images/first_16.png If the dimensions for the sample faces don't work out to ( 361 x 361 ) in the end, then you are likely to be missing a transpose somewhere. Also, be aware that the scipy linalg.eig by default returns a vector of eigenvalues and a matrix, but the Matlab eig(), returns 2 matrices ( the eigenvalues are multiplied by an identity matrix to get a diagonal matrix). You can check out the mathesaurus reference sheet for help translating the example into python, but hopefully this will point you in the right direction: see: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/eig.html vs:
On Sat, Mar 1, 2008 at 12:41 AM, devnew@gmail.com <devnew@gmail.com> wrote:
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
![](https://secure.gravatar.com/avatar/85d21d92c30656f51fc4281f861ad5b2.jpg?s=120&d=mm&r=g)
The steps you describe here are correct. I am putting together an open source computer vision library based on numpy/scipy. It will include an automatic PCA algorithm with face detection, eye detection, PCA dimensionally reduction, and distance measurement. If you are interested let me know and I will redouble my efforts to release the code soon. Dave On Feb 29, 2008, at 12:15 PM, devnew@gmail.com wrote:
![](https://secure.gravatar.com/avatar/d5aa64affb55c3d87e1f4d675ee483f0.jpg?s=120&d=mm&r=g)
Am Dienstag, 11. März 2008 00:24:04 schrieb David Bolme:
That's interesting, we're also working on a computer vision module using NumPy (actually, a VIGRA <-> NumPy binding sort of), and there's scipy.ndimage, too. Maybe (part of) your code could be integrated into the latter? I am looking forward to it anyway. -- Ciao, / / /--/ / / ANS
![](https://secure.gravatar.com/avatar/85d21d92c30656f51fc4281f861ad5b2.jpg?s=120&d=mm&r=g)
If you are interested I now have the code on source forge. It still needs some critical documentation. I am planing to get this documented and a beta release sometime this summer. Currently it ties together PIL, OpenCV, numpy/scipy, LibSVM, and some of own code with an emphases on face recognition since that is my research area. http://pyvision.sourceforge.net On Apr 7, 2008, at 3:40 AM, Hans Meine wrote:
![](https://secure.gravatar.com/avatar/742736902afc4e0926b96932ada8cbc1.jpg?s=120&d=mm&r=g)
RoyG, The timing of your question couldn't be better, I just did an blog post on this (I also plugged scipy and the EPD): http://www.datawrangling.com/python-montage-code-for-displaying-arrays.html The code basically replicates the matlab montage() function and approach to handling grayscale images using matplotlib. -Pete On Fri, Feb 29, 2008 at 2:15 PM, devnew@gmail.com <devnew@gmail.com> wrote:
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
![](https://secure.gravatar.com/avatar/742736902afc4e0926b96932ada8cbc1.jpg?s=120&d=mm&r=g)
Here is the page I referenced for the octave version ... it includes examples very similar to what you want. I will be posting a very similar example in Python later this month. I don't have any Python code on hand for the Petland paper, but I think matlab example should be easy to translate to scipy/matplotlib using the montage function: load faces.mat %Form covariance matrix C=cov(faces'); %build eigenvectors and eigenvalues [E,D] = eig(C); %sort based on eigenvalue [B,index] = sortrows(D'); E2=E'; E2(index,:)'; eigensorted=E2(index,:)'; %show eigenfaces clear Z; for i=1:length(eigensorted) Z(:,:,1,i)=reshape(eigensorted(:,i)-1.5*min(min(min(eigensorted))), 19,19); end montage(Z) %show top 16 eigenfaces clear Z; for i=1:16 Z(:,:,1,i)=reshape(eigensorted(:,i)-min(min(min(eigensorted))), 19,19); end montage(Z) On Fri, Feb 29, 2008 at 2:50 PM, Peter Skomoroch <peter.skomoroch@gmail.com> wrote:
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
![](https://secure.gravatar.com/avatar/742736902afc4e0926b96932ada8cbc1.jpg?s=120&d=mm&r=g)
Forgot the url: http://www.cis.hut.fi/Opinnot/T-61.2010/harjoitustyo_en07.shtml On Fri, Feb 29, 2008 at 2:56 PM, Peter Skomoroch <peter.skomoroch@gmail.com> wrote:
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
![](https://secure.gravatar.com/avatar/559e84125b0b3f9282adfa291cd5b019.jpg?s=120&d=mm&r=g)
On Mar 1, 12:57 am, "Peter Skomoroch" wrote: I think
hi Peter, nice code..ran the examples.. however couldn't follow the matlab code since i have no exposure to matlab..was using numpy etc for calcs could you confirm the layout for the face images data? i assumed that the initial face matrix should be faces=a numpy matrix with N rows ie N=numofimages row1=image1pixels as a sequence row2=image2pixels as a sequence ... rowN=imageNpixels as a sequence and covariancematrix=faces*faces_transpose is this the right way? thanks
![](https://secure.gravatar.com/avatar/742736902afc4e0926b96932ada8cbc1.jpg?s=120&d=mm&r=g)
I think that is correct... Here is what the final result should look like: http://www.datawrangling.com/media/images/first_16.png If the dimensions for the sample faces don't work out to ( 361 x 361 ) in the end, then you are likely to be missing a transpose somewhere. Also, be aware that the scipy linalg.eig by default returns a vector of eigenvalues and a matrix, but the Matlab eig(), returns 2 matrices ( the eigenvalues are multiplied by an identity matrix to get a diagonal matrix). You can check out the mathesaurus reference sheet for help translating the example into python, but hopefully this will point you in the right direction: see: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/eig.html vs:
On Sat, Mar 1, 2008 at 12:41 AM, devnew@gmail.com <devnew@gmail.com> wrote:
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
![](https://secure.gravatar.com/avatar/85d21d92c30656f51fc4281f861ad5b2.jpg?s=120&d=mm&r=g)
The steps you describe here are correct. I am putting together an open source computer vision library based on numpy/scipy. It will include an automatic PCA algorithm with face detection, eye detection, PCA dimensionally reduction, and distance measurement. If you are interested let me know and I will redouble my efforts to release the code soon. Dave On Feb 29, 2008, at 12:15 PM, devnew@gmail.com wrote:
![](https://secure.gravatar.com/avatar/d5aa64affb55c3d87e1f4d675ee483f0.jpg?s=120&d=mm&r=g)
Am Dienstag, 11. März 2008 00:24:04 schrieb David Bolme:
That's interesting, we're also working on a computer vision module using NumPy (actually, a VIGRA <-> NumPy binding sort of), and there's scipy.ndimage, too. Maybe (part of) your code could be integrated into the latter? I am looking forward to it anyway. -- Ciao, / / /--/ / / ANS
![](https://secure.gravatar.com/avatar/85d21d92c30656f51fc4281f861ad5b2.jpg?s=120&d=mm&r=g)
If you are interested I now have the code on source forge. It still needs some critical documentation. I am planing to get this documented and a beta release sometime this summer. Currently it ties together PIL, OpenCV, numpy/scipy, LibSVM, and some of own code with an emphases on face recognition since that is my research area. http://pyvision.sourceforge.net On Apr 7, 2008, at 3:40 AM, Hans Meine wrote:
participants (4)
-
David Bolme
-
devnew@gmail.com
-
Hans Meine
-
Peter Skomoroch