您可以使用函数将新的输入面投影到特征空间上subspaceProject()
,然后使用从特征空间生成重建的面subspaceReconstruct()
,然后比较input_face和reconstructed_face的相似程度。已知人脸(训练数据集中的人脸)的重建图像与input_face的相似性高于冒名顶替者的人脸。您可以为验证设置相似度阈值。这是代码:
// Project the input face onto the eigenspace.
Mat projection = subspaceProject(eigenvectors, FaceRow,input_face.reshape(1,1));
//Generate the reconstructed face
Mat reconstructionRow = subspaceReconstruct(eigenvectors,FaceRow, projection);
// Reshape the row mat to an image mat
Mat reconstructionMat = reconstructionRow.reshape(1,faceHeight);
// Convert the floating-point pixels to regular 8-bit uchar.
Mat reconstructed_face = Mat(reconstructionMat.size(), CV_8U);
reconstructionMat.convertTo(reconstructed_face, CV_8U, 1, 0);
然后,您可以使用 比较输入人脸和重建人脸cv::norm()
。例如:
// Calculate the L2 relative error between the 2 images.
double err = norm(input_face,reconstructed_face, CV_L2);
// Convert to a reasonable scale
double similarity = error / (double)(input_face.rows * input_face.cols);