Search code examples
pythonopencvface-recognition

How to get best eigenface using opencv python


I am using this link : https://www.learnopencv.com/eigenface-using-opencv-c-python/ to get the average face of the images and eigenfaces.

# Compute the eigenvectors from the stack of images created
  print("Calculating PCA ", end="...")
  mean, eigenVectors = cv2.PCACompute(data, mean=None, maxComponents=NUM_EIGEN_FACES)
  print ("DONE")
  averageFace = mean.reshape(sz)
  eigenFaces = [];
  for eigenVector in eigenVectors:
    eigenFace = eigenVector.reshape(sz)
    eigenFaces.append(eigenFace)

But I don't know how to get top eigenfaces like this picture image example top eigenfaces

Source code github


Solution

  • Eigen faces are sorted in eigenVector from top to bottom. ( by eigen values, from largest to smallest ). So just out first N eigen faces. Usually to show we need to add mean face to eigen faces and show resulting images.

    Really eigen faces are ND axes in face image space, mean face is origin, eigen values are proportional to dispersion of input faces set along certain axis (eigen face). First ("best" in your case) is the axis with maximum variance.

    So if thinkking in face space, the face is a point in eigen space and you can sinthesize any face as usual point

    face(C1,C2,..,CN)=mean_face+ C1*eigen_face1+C2*eigen_face2+...CN*eigen_faceN

    You can aso project any face to eigen space and get it's C1,C2,...,CN coordinates.

    Using these coordinates you can find "distance" between faces and it usually used to compare faces.