how to create eigenface image

harryos oswald.harry at gmail.com
Wed Feb 27 19:05:36 CET 2008


hi all
I am new to python and learning PCA method by reading up Turk&Petland
papers etc
while trying out PCA on a set of greyscale images using python, and
numpy I tried to create eigenvectors and facespace.

i have
 facesarray--- an NXP numpy.ndarray that contains data of images
       N=numof images,P=pixels in an image
avgarray --1XP array containing avg value for each pixel
 adjustedfaces=facesarray-avgarray
adjustedmatrix=matrix(adjustedfaces)
adjustedmatrix_trans=adjustedmatrix.transpose()
covariancematrix =adjustedmatrix*adjustedmatrix_trans
evalues,evect=eigh(covariancematrix)

after sorting such that most significant eigenvectors are selected
evectmatrix is now my eigenvectors matrix

here is a sample using 4X3 greyscale images

evalues
[ -1.85852801e-13   6.31143639e+02   3.31182765e+03   5.29077871e+03]
evect
[[ 0.5        -0.06727772  0.6496399  -0.56871936]
 [ 0.5        -0.77317718 -0.37697426  0.10043632]
 [ 0.5         0.27108233  0.31014514  0.76179023]
 [ 0.5         0.56937257 -0.58281078 -0.29350719]]

evectmatrix  (sorted according to largest evalue first)
[[-0.56871936  0.6496399  -0.06727772  0.5       ]
 [ 0.10043632 -0.37697426 -0.77317718  0.5       ]
 [ 0.76179023  0.31014514  0.27108233  0.5       ]
 [-0.29350719 -0.58281078  0.56937257  0.5       ]]



then i can create facespace by
facespace=evectmat*adjustedfaces

what i want to know is how i can create eigenface images .
when you mean an eigenvector ,is that a row of evectmatrix above?  is
evectmatrix[0] the eigenvector for first image and so on? I would
appreciate if someone can make this clear.

also
when i tried to make an image by
im=im = Image.new('L', (width,height))
im.putdata(evectmat[0])
im.save("eigenface0.jpg")

it created an image with all pixels dark with no details to see
the same occurrs if i use
im.putdata(facespace[0])
can someone tell me if i am doing it wrong..







More information about the Python-list mailing list