人脸识别

╰半夏微凉° 2022-08-08 05:16 525阅读 0赞

计划 实现了一个基于 PCA 的人脸识别 方法 ,我 称之为 “ 特征点方法 ”, 所有的功能简单而且实用 。
下面,我使用一个简单的MATLAB脚本 说明 它的用法 。
一般情况 , 你应该 按照以下这个顺序执行这个方法 :

  1. 选择实际 测试和参照组人脸图像 数据库 的路径;
  2. 选择 实际测试人脸图像的 路径;
  3. 运行 “ CreateDatabase ” 函数来创建 所有参照组人脸图像 的二维 矩阵;
  4. 运行 “ eigenfacecore”函数 产生 基础 人脸图像空间;
  5. 运行 “识别” 功能得到 参照组人脸图像数据库中 等价图像的 名称 。
    为了您的方便 , 我准备了 实际 测试和参照组人脸图像数据库 , 其中 部分来自 “Face94”埃塞克斯人脸数据库。
    你只 需要复制 上述功能 ,指定 实际 测试和参照组人脸图像 数据库 的路径 ( 比如Matlab 工作 路径 )。
    然后按照 对话框提示输入图片编号 ,实例将运行实现 。

希望您能喜欢它!微笑微笑

引用:

[1] P. N. Belhumeur, J. Hespanha, and D. J. Kriegman. Eigenfaces vs. Fisherfaces: Recognition
using class specific linear projection. In ECCV (1), pages 45—58, 1996.
[2] Available at:
http://cswww.essex.ac.uk/mv/allfaces/faces94.zip

以下为源代码文件:

-—————————————————————————————————————————————————————————————————

CreateDatabase.m

  1. function T = CreateDatabase(TrainDatabasePath)
  2. % Align a set of face images (the training set T1, T2, ... , TM )
  3. %
  4. % Description: This function reshapes all 2D images of the training database
  5. % into 1D column vectors. Then, it puts these 1D column vectors in a row to
  6. % construct 2D matrix 'T'.
  7. %
  8. %
  9. % Argument: TrainDatabasePath - Path of the training database
  10. %
  11. % Returns: T - A 2D matrix, containing all 1D image vectors.
  12. % Suppose all P images in the training database
  13. % have the same size of MxN. So the length of 1D
  14. % column vectors is MN and 'T' will be a MNxP 2D matrix.
  15. %
  16. % See also: STRCMP, STRCAT, RESHAPE
  17. % Original version by Amir Hossein Omidvarnia, October 2007
  18. % Email: aomidvar@ece.ut.ac.ir
  19. %%%%%%%%%%%%%%%%%%%%%%%% File management
  20. TrainFiles = dir(TrainDatabasePath);
  21. Train_Number = 0;
  22. for i = 1:size(TrainFiles,1)
  23. if not(strcmp(TrainFiles(i).name,'.')|strcmp(TrainFiles(i).name,'..')|strcmp(TrainFiles(i).name,'Thumbs.db'))
  24. Train_Number = Train_Number + 1; % Number of all images in the training database
  25. end
  26. end
  27. %%%%%%%%%%%%%%%%%%%%%%%% Construction of 2D matrix from 1D image vectors
  28. T = [];
  29. for i = 1 : Train_Number
  30. % I have chosen the name of each image in databases as a corresponding
  31. % number. However, it is not mandatory!
  32. str = int2str(i);
  33. str = strcat('\',str,'.jpg');
  34. str = strcat(TrainDatabasePath,str);
  35. img = imread(str);
  36. img = rgb2gray(img);
  37. [irow icol] = size(img);
  38. temp = reshape(img',irow*icol,1); % Reshaping 2D images into 1D image vectors
  39. T = [T temp]; % 'T' grows after each turn
  40. end

-—————————————————————————————————————————————————————————————

EigenfaceCore.m

  1. function [m, A, Eigenfaces] = EigenfaceCore(T)
  2. % Use Principle Component Analysis (PCA) to determine the most
  3. % discriminating features between images of faces.
  4. %
  5. % Description: This function gets a 2D matrix, containing all training image vectors
  6. % and returns 3 outputs which are extracted from training database.
  7. %
  8. % Argument: T - A 2D matrix, containing all 1D image vectors.
  9. % Suppose all P images in the training database
  10. % have the same size of MxN. So the length of 1D
  11. % column vectors is M*N and 'T' will be a MNxP 2D matrix.
  12. %
  13. % Returns: m - (M*Nx1) Mean of the training database
  14. % Eigenfaces - (M*Nx(P-1)) Eigen vectors of the covariance matrix of the training database
  15. % A - (M*NxP) Matrix of centered image vectors
  16. %
  17. % See also: EIG
  18. % Original version by Amir Hossein Omidvarnia, October 2007
  19. % Email: aomidvar@ece.ut.ac.ir
  20. %%%%%%%%%%%%%%%%%%%%%%%% Calculating the mean image
  21. m = mean(T,2); % Computing the average face image m = (1/P)*sum(Tj's) (j = 1 : P)
  22. Train_Number = size(T,2);
  23. %%%%%%%%%%%%%%%%%%%%%%%% Calculating the deviation of each image from mean image
  24. A = [];
  25. for i = 1 : Train_Number
  26. temp = double(T(:,i)) - m; % Computing the difference image for each image in the training set Ai = Ti - m
  27. A = [A temp]; % Merging all centered images
  28. end
  29. %%%%%%%%%%%%%%%%%%%%%%%% Snapshot method of Eigenface methos
  30. % We know from linear algebra theory that for a PxQ matrix, the maximum
  31. % number of non-zero eigenvalues that the matrix can have is min(P-1,Q-1).
  32. % Since the number of training images (P) is usually less than the number
  33. % of pixels (M*N), the most non-zero eigenvalues that can be found are equal
  34. % to P-1. So we can calculate eigenvalues of A'*A (a PxP matrix) instead of
  35. % A*A' (a M*NxM*N matrix). It is clear that the dimensions of A*A' is much
  36. % larger that A'*A. So the dimensionality will decrease.
  37. L = A'*A; % L is the surrogate of covariance matrix C=A*A'.
  38. [V D] = eig(L); % Diagonal elements of D are the eigenvalues for both L=A'*A and C=A*A'.
  39. %%%%%%%%%%%%%%%%%%%%%%%% Sorting and eliminating eigenvalues
  40. % All eigenvalues of matrix L are sorted and those who are less than a
  41. % specified threshold, are eliminated. So the number of non-zero
  42. % eigenvectors may be less than (P-1).
  43. L_eig_vec = [];
  44. for i = 1 : size(V,2)
  45. if( D(i,i)>1 )
  46. L_eig_vec = [L_eig_vec V(:,i)];
  47. end
  48. end
  49. %%%%%%%%%%%%%%%%%%%%%%%% Calculating the eigenvectors of covariance matrix 'C'
  50. % Eigenvectors of covariance matrix C (or so-called "Eigenfaces")
  51. % can be recovered from L's eiegnvectors.
  52. Eigenfaces = A * L_eig_vec; % A: centered image vectors

-———————————————————————————————————————————————————————————————

Recognition.m

  1. function OutputName = Recognition(TestImage, m, A, Eigenfaces)
  2. % Recognizing step....
  3. %
  4. % Description: This function compares two faces by projecting the images into facespace and
  5. % measuring the Euclidean distance between them.
  6. %
  7. % Argument: TestImage - Path of the input test image
  8. %
  9. % m - (M*Nx1) Mean of the training
  10. % database, which is output of 'EigenfaceCore' function.
  11. %
  12. % Eigenfaces - (M*Nx(P-1)) Eigen vectors of the
  13. % covariance matrix of the training
  14. % database, which is output of 'EigenfaceCore' function.
  15. %
  16. % A - (M*NxP) Matrix of centered image
  17. % vectors, which is output of 'EigenfaceCore' function.
  18. %
  19. % Returns: OutputName - Name of the recognized image in the training database.
  20. %
  21. % See also: RESHAPE, STRCAT
  22. % Original version by Amir Hossein Omidvarnia, October 2007
  23. % Email: aomidvar@ece.ut.ac.ir
  24. %%%%%%%%%%%%%%%%%%%%%%%% Projecting centered image vectors into facespace
  25. % All centered images are projected into facespace by multiplying in
  26. % Eigenface basis's. Projected vector of each face will be its corresponding
  27. % feature vector.
  28. ProjectedImages = [];
  29. Train_Number = size(Eigenfaces,2);
  30. for i = 1 : Train_Number
  31. temp = Eigenfaces'*A(:,i); % Projection of centered images into facespace
  32. ProjectedImages = [ProjectedImages temp];
  33. end
  34. %%%%%%%%%%%%%%%%%%%%%%%% Extracting the PCA features from test image
  35. InputImage = imread(TestImage);
  36. temp = InputImage(:,:,1);
  37. [irow icol] = size(temp);
  38. InImage = reshape(temp',irow*icol,1);
  39. Difference = double(InImage)-m; % Centered test image
  40. ProjectedTestImage = Eigenfaces'*Difference; % Test image feature vector
  41. %%%%%%%%%%%%%%%%%%%%%%%% Calculating Euclidean distances
  42. % Euclidean distances between the projected test image and the projection
  43. % of all centered training images are calculated. Test image is
  44. % supposed to have minimum distance with its corresponding image in the
  45. % training database.
  46. Euc_dist = [];
  47. for i = 1 : Train_Number
  48. q = ProjectedImages(:,i);
  49. temp = ( norm( ProjectedTestImage - q ) )^2;
  50. Euc_dist = [Euc_dist temp];
  51. end
  52. [Euc_dist_min , Recognized_index] = min(Euc_dist);
  53. OutputName = strcat(int2str(Recognized_index),'.jpg');

-—————————————————————————————————————————————————————

example.m

  1. % A sample script, which shows the usage of functions, included in
  2. % PCA-based face recognition system (Eigenface method)
  3. %
  4. % See also: CREATEDATABASE, EIGENFACECORE, RECOGNITION
  5. % Original version by Amir Hossein Omidvarnia, October 2007
  6. % Email: aomidvar@ece.ut.ac.ir
  7. clear all
  8. clc
  9. close all
  10. % You can customize and fix initial directory paths
  11. TrainDatabasePath = uigetdir('D:\MATLAB701\work', 'D:\MATLAB701\work\TrainDatabase' );
  12. TestDatabasePath = uigetdir('D:\MATLAB701\work', 'D:\MATLAB701\work\TestDatabase');
  13. prompt = {'1:'};
  14. dlg_title = 'Input of PCA-Based Face Recognition System';
  15. num_lines= 1;
  16. def = {'1'};
  17. TestImage = inputdlg(prompt,dlg_title,num_lines,def);
  18. TestImage = strcat(TestDatabasePath,'\',char(TestImage),'.jpg');
  19. im = imread(TestImage);
  20. T = CreateDatabase(TrainDatabasePath);
  21. [m, A, Eigenfaces] = EigenfaceCore(T);
  22. OutputName = Recognition(TestImage, m, A, Eigenfaces);
  23. SelectedImage = strcat(TrainDatabasePath,'\',OutputName);
  24. SelectedImage = imread(SelectedImage);
  25. imshow(im)
  26. title('Test Image');
  27. figure,imshow(SelectedImage);
  28. title('Equivalent Image');
  29. str = strcat('Matched image is : ',OutputName);
  30. disp(str)

用到的图像库文件:

TestDatabase

CenterCenter 1Center 2Center 3Center 4Center 5Center 6Center 7Center 8Center 9

TrainDatabase

Center 10Center 11Center 12Center 13Center 14Center 15Center 16Center 17Center 18Center 19Center 20Center 21Center 22Center 23Center 24Center 25Center 26Center 27Center 28

发表评论

表情:
评论列表 (有 0 条评论,525人围观)

还没有评论,来说两句吧...

相关阅读

    相关 人脸识别

    计划 实现了一个基于 PCA 的人脸识别 方法 ,我 称之为 “ 特征点方法 ”, 所有的功能简单而且实用 。 下面,我使用一个简单的MATLAB脚本 说明 它的用法

    相关 人脸识别

    人脸检测 [长文干货!走近人脸检测:从 VJ 到深度学习(上)][VJ] [长文干货!走近人脸检测:从VJ到深度学习(下)][VJ 1]

    相关 人脸识别系统_人脸注册

        基于上次的人脸检测后,一直纠结人脸注册,照片存放方式,我想到了两种方式,1.数据库存照片存放的路径,2.数据库存放照片的二进制码。但是针对我的毕业设计我想要是存路径的话

    相关 人脸识别系统_人脸检测

    项目:基于人脸识别的无卡ATM机模拟系统 主要实现内容: 包括实现AMT机模拟人脸识别和密码输入、PC端模拟实现储户数据库服务器系统。 1. ATM模拟端实现采用手

    相关 人脸识别杂谈

    Gabor 及 LBP 特征描述子是迄今为止在人脸识别领域最为成功的两种人工设计局部描述子。 对各种人脸识别影响因子的针对性处理也是那一阶段的研究热点,比如人脸光照归一化、人