




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
PCA人脸识别算法Matlab版2010-11-18 19:301.数据准备function T=CreateDatabase(TrainDatabasePath)%Align a set of face images (the training set T1, T2, . , TM )%Description: This function reshapes all 2D images of the training database%into 1D column vectors. Then, it puts these 1D column vectors in a row to%construct 2D matrixT.%Argument: TrainDatabasePath-Path of the training database%Returns: T-A 2D matrix, containing all 1D image vectors.% Suppose all P images in the training database% have the same size of MxN. So the length of 1D% column vectors is MN andTwill be a MNxP 2D matrix.%See also: STRCMP, STRCAT, RESHAPE%Original version by Amir Hossein Omidvarnia, October2007% Email: aomidvarece.ut.ac.ir%File managementTrainFiles=dir(TrainDatabasePath);Train_Number=0;fori=1:size(TrainFiles,1)ifnot(strcmp(TrainFiles(i).name,.)|strcmp(TrainFiles(i).name,.)|strcmp(TrainFiles(i).name,Thumbs.db) Train_Number=Train_Number+1;%Number of all images in the training database endend%Construction of 2D matrix from 1D image vectorsT=;fori=1: Train_Number%I have chosen the name of each image in databases as a corresponding%number. However, it is not mandatory! str=int2str(i); str=strcat(,str,.jpg); str=strcat(TrainDatabasePath,str); img=imread(str); img=rgb2gray(img); irow icol=size(img); temp=reshape(img,irow*icol,1); % Reshaping 2D images into 1D image vectors T=T temp;%Tgrows after each turnend2, 样本训练function m, A, Eigenfaces=EigenfaceCore(T)%Use Principle Component Analysis (PCA) to determine the most%discriminating features between images of faces.%Description: This function gets a 2D matrix, containing all training image vectors%and returns3outputs which are extracted from training database.%Argument: T-A 2D matrix, containing all 1D image vectors.% Suppose all P images in the training database% have the same size of MxN. So the length of 1D% column vectors is M*N andTwill be a MNxP 2D matrix.%Returns: m-(M*Nx1) Mean of the training database% Eigenfaces-(M*Nx(P-1) Eigen vectors of the covariance matrix of the training database% A-(M*NxP) Matrix of centered image vectors%See also: EIG%Original version by Amir Hossein Omidvarnia, October2007% Email: aomidvarece.ut.ac.ir%Calculating the mean imagem=mean(T,2);%Computing the average face image m=(1/P)*sum(Tjs) (j = 1 : P)Train_Number=size(T,2);%Calculating the deviation of each image from mean imageA=;fori=1: Train_Number temp=double(T(:,i)-m;%Computing the difference imageforeach image in the training set Ai=Ti-m A=A temp;%Merging all centered imagesend%Snapshot method of Eigenface methos%We know from linear algebra theory thatfora PxQ matrix, the maximum%number of non-zero eigenvalues that the matrix can have is min(P-1,Q-1).%Since the number of training images (P) is usually less than the number%of pixels (M*N), the most non-zero eigenvalues that can be found are equal%to P-1. So we can calculate eigenvalues of A*A (a PxP matrix) instead of%A*A(a M*NxM*N matrix). It is clear that the dimensions of A*Ais much%larger that A*A. So the dimensionality will decrease.L=A*A; % L is the surrogate of covariance matrix C=A*A.V D=eig(L);%Diagonal elements of D are the eigenvaluesforboth L=A*A and C=A*A.%Sorting and eliminating eigenvalues%All eigenvalues of matrix L are sorted and those who are less than a%specified threshold, are eliminated. So the number of non-zero%eigenvectors may be less than (P-1).L_eig_vec=;fori=1: size(V,2)if( D(i,i)1) L_eig_vec=L_eig_vec V(:,i); endend%Calculating the eigenvectors of covariance matrixC%Eigenvectors of covariance matrix C (or so-calledEigenfaces)%can be recovered from Ls eiegnvectors.Eigenfaces=A*L_eig_vec;%A: centered image vectors3. 图像识别function OutputName=Recognition(TestImage, m, A, Eigenfaces)%Recognizing step.%Description: This function compares two faces by projecting the images into facespace and%measuring the Euclidean distance between them.%Argument: TestImage-Path of the input test image% m-(M*Nx1) Mean of the training% database, which is output ofEigenfaceCorefunction.% Eigenfaces-(M*Nx(P-1) Eigen vectors of the% covariance matrix of the training% database, which is output ofEigenfaceCorefunction.% A-(M*NxP) Matrix of centered image% vectors, which is output ofEigenfaceCorefunction.%Returns: OutputName-Name of the recognized image in the training database.%See also: RESHAPE, STRCAT%Original version by Amir Hossein Omidvarnia, October2007% Email: aomidvarece.ut.ac.ir%Projecting centered image vectors into facespace%All centered images are projected into facespace by multiplying in%Eigenface basiss. Projected vector of each face will be its corresponding%feature vector.ProjectedImages=;Train_Number=size(Eigenfaces,2);fori=1: Train_Number temp=Eigenfaces*A(:,i); % Projection of centered images into facespace ProjectedImages=ProjectedImages temp;end%Extracting the PCA features from test imageInputImage=imread(TestImage);temp=InputImage(:,:,1);irow icol=size(temp);InImage=reshape(temp,irow*icol,1);Difference=double(InImage)-m;%Centered test imageProjectedTestImage=Eigenfaces*Difference; % Test image feature vector%Calculating Euclidean distances%Euclidean distances between the projected test image and the projection%of all centered training images are calculated. Test image is%supposed to have minimum distance with its corresponding image in the%training database.Euc_dist=;fori=1: Train_Number q=ProjectedImages(:,i); temp=( norm( ProjectedTestImage-q ) )2; Euc_dist=Euc_dist temp;endEuc_dist_min , Recognized_index=min(Euc_dist);OutputName=strcat(int2str(Recognized_index),.jpg);协方差矩阵的详细说明2010-11-18 18:59转载自vicki_mm最终编辑遗落天际边变量说明:设为一组随机变量,这些随机变量构成随机向量,每个随机变量有m个样本,则有样本矩阵(1)其中对应着每个随机向量X的样本向量,对应着第
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 数控线切割操作工应知考核试题及参考答案
- 2025年教育行业教育信息化建设报告:教育信息化建设策略与实施
- 2025年教师信息技术与课程整合应用研究报告
- 2025年文化与娱乐产业跨界合作案例分析报告
- 2025年物联网设备安全漏洞应对与防护策略深度解读报告
- 押题宝典执业药师资格证之《西药学专业二》试题附答案详解【b卷】
- 解析卷沪科版9年级下册期末试题含答案详解(考试直接用)
- 2025年农业科技产品区域代理合作协议书
- 2025年度酒店客房清洁外包服务承包合同
- 2025年度高品质桶装水配送服务及运输合作协议
- (2025秋新修订)人教版三年级数学上册全册教案(教学设计)
- 新版人教版二年级上册数学全册1-6单元教材分析
- 期中考试考试安排及流程说明
- 铜矿采选工程可行性研究报告
- 2024-2025学年北京市海淀区三年级(下)期末数学试卷
- 大型展会现场安全保障工作方案
- 2025年秋季学期学校全面工作计划
- 2024-2030年钻石行业市场调研及前景趋势预测报告
- 老高糖健康管理培训课件
- 璧山区辅警考试题及答案
- 【经典】高一英语完形填空专题练习及答案共20篇
评论
0/150
提交评论