找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2650|回复: 0
收起左侧

人脸识别2dpca的matlab源码

[复制链接]
ID:374393 发表于 2018-7-18 20:58 | 显示全部楼层 |阅读模式
人脸识别程序,欢迎大家参考

2dpca的matlab源程序:
  1. function [eigvectors, eigvalues, meanData, newTrainData, newTestData] = TDPCA(trainData, testData, height, width, numvecs)
  2. %2DPCA        Two Dimensional Principal component analysis
  3. %        Usage:
  4. %        [eigvectors, eigvalues, meanData, newTrainData, newTestData] = TDPCA(trainData, testData, height, width, numvecs)
  5. %
  6. %        trainData: Rows of vectors of training data points
  7. %   testData: Rows of vectors of testing data points
  8. %   height: height of the image matrix
  9. %   width: width of the image matrix
  10. %   numvecs: the needed number of eigenvectors
  11. %         
  12. %   meanData: Mean of all the data.
  13. %        newTrainData: The data after projection (mean removed)
  14. %   newTestData: The data after projection (mean removed)
  15. %        eigvectors: Each column of this matrix is a eigenvector of the convariance
  16. %                   matrix defined in 2DPCA
  17. %        eigvalues: Eigenvalues of the convariance matrix
  18. %
  19. %
  20. %   Reference paper: J.Yang,D.Zhang,A.F.Frangi,and J.Yang.Two-dimensional
  21. %                    pca:A new approach to a appearance-based face
  22. %                    represenation and recognition. IEEE Trans.on
  23. %                    PAMI,2004
  24. %   Written by Zhonghua Shen (cnjsnt_s@yahoo.com.cn), 2006.07

  25. % Check arguments

  26. if nargin ~= 5
  27.     error('usage: [eigvectors, eigvalues, meanData, newTrainData, newTestData] = TDPCA(trainData, testData, height, width, numvecs)');
  28. end;

  29. [nSam,nFea] = size(trainData);

  30. fprintf(1,'Computing average matrix...\n');
  31. meanDataVector = mean(trainData);
  32. meanData = reshape(meanDataVector,height,width);

  33. fprintf(1,'Calculating matrix differences from avg and 2DPCA covariance matrix L...\n');
  34. L = zeros(width,width);
  35. ddata = zeros(nSam,nFea);
  36. for i = 1:nSam
  37.     ddata(i,:) = trainData(i,:)-meanDataVector;
  38.     dummyMat = reshape(ddata(i,:),height,width);
  39.     L = L + dummyMat'*dummyMat;
  40. end;
  41. L = L/nSam;
  42. L = (L + L')/2;


  43. fprintf(1,'Calculating eigenvectors of L...\n');
  44. [eigvectors,eigvalues] = eig(L);

  45. fprintf(1,'Sorting eigenvectors according to eigenvalues...\n');
  46. [eigvectors,eigvalues] = sortem(eigvectors,eigvalues);
  47. eigvalues = diag(eigvalues);

  48. fprintf(1,'Normalize Vectors to unit length, kill vectors corr. to tiny evalues...\n');
  49. num_good = 0;
  50. for i = 1:size(eigvectors,2)
  51.     eigvectors(:,i) = eigvectors(:,i)/norm(eigvectors(:,i));
  52.     if eigvalues(i) < 0.00001
  53.         % Set the vector to the 0 vector; set the value to 0.
  54.         eigvalues(i) = 0;
  55.         eigvectors(:,i) = zeros(size(eigvectors,1),1);
  56.     else
  57.         num_good = num_good + 1;
  58.     end;
  59. end;
  60. if (numvecs > num_good)
  61.     fprintf(1,'Warning: numvecs is %d; only %d exist.\n',numvecs,num_good);
  62.     numvecs = num_good;
  63. end;
  64. eigvectors = eigvectors(:,1:numvecs);

  65. if nargout == 5
  66. fprintf(1,'Feature extraction and calculating new training and testing data...\n');
  67. newTrainData = zeros(nSam,height*numvecs);
  68. for i = 1:nSam
  69.     dummyMat = reshape(ddata(i,:),height,width);
  70.     newTrainData(i,:) = reshape(dummyMat*eigvectors,1,height*numvecs);
  71. end

  72. %testData
  73. ……………………

  74. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码


全部资料51hei下载地址:
2dpca的matlab源代码.rar (1.22 KB, 下载次数: 52)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表