matlab实现基于主成分分析的人脸识别
MATLAB代码如下
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220function varargout = face_recognition(varargin) % FACE_RECOGNITION MATLAB code for face_recognition.fig % FACE_RECOGNITION, by itself, creates a new FACE_RECOGNITION or raises the existing % singleton*. % % H = FACE_RECOGNITION returns the handle to a new FACE_RECOGNITION or the handle to % the existing singleton*. % % FACE_RECOGNITION('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in FACE_RECOGNITION.M with the given input arguments. % % FACE_RECOGNITION('Property','Value',...) creates a new FACE_RECOGNITION or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before face_recognition_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to face_recognition_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help face_recognition % Last Modified by GUIDE v2.5 20-Sep-2017 23:33:17 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @face_recognition_OpeningFcn, ... 'gui_OutputFcn', @face_recognition_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before face_recognition is made visible. function face_recognition_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to face_recognition (see VARARGIN) % Choose default command line output for face_recognition handles.output = hObject; % Update handles structure guidata(hObject, handles); clear all; global coeff global scores global training_count global samples_mean %% %%训练, % 人脸库中的15个人来自Yale人脸数据库,另外还采集了2个人的样本,每个人脸的样本数量为11,(可以根据需要再采集一些人脸样本,命名规则为 0xx/xx.jpg)。 %输入人脸样本数 people_count = input('请输入样本数,(输入0为默认值15,当前样本库有18个人) : '); fprintf('您输入的样本数为: %d nn',people_count); %每类样本数 face_count_per_people=11; %输入每类训练样本比例 %training_ratio=.20; training_ratio = input('请输入每类训练样本比例(输入范围为0-1): '); if training_ratio< 1 && training_ratio>0 fprintf('您输入的每类训练样本比例为: %.1f nn',training_ratio); else fprintf('您输入的每类训练样本有误,系统取默认值为0.5 nn'); training_ratio = 0.5; end disp('Waiting for training.......'); % 能量,即较大特征值之和占所有特征值之和的比例。 energy=0.9; % 每类训练样本数 training_count=floor(face_count_per_people*training_ratio); %floor向后取整,training_ratio=0.7时,training_count为7 %训练样本数据,每行是一个样本 training_samples=[]; % 训练 for i=1:people_count for j=1:training_count img=im2double(imread(['样本库' sprintf('%03d',i) '' sprintf('%02d',j) '.jpg']));%读取的地址为 0xx/xx.jpg img=imresize(img,[10 10]); % 图像缩放至至10*10 %若是彩色图像,则灰度化 if ndims(img)==3 img=rgb2gray(img); end training_samples=[training_samples;img(:)'];%将缩放后的10*10图像转为一行,一行代表一副图片的信息,并将其放入training_samples中 end end %求取训练样本均值,每行一个样本,每列一类特征 samples_mean=mean(training_samples);%对每一列取平均,得到每个字段的平均值 %whos training_samples %调用matlab的pca函数 %coeff是主成分系数矩阵,即变换(投影)矩阵 %scores是训练样本投影后的矩阵 %latent是协方差矩阵的特征值,降序排列 [coeff,scores,latent]=pca(training_samples);%对样本进行求协方差矩阵,求平均值和协方差 % whos coeff%100*100 %10*10的图片变换成一行后是100维,training_samples为 [people_count*training_count 100] 其协方差矩阵的特征向量即为coeff是100*100 % whos latent%100*1 %寻找占了energy比例的下标,即主成分就取到这么多维 idx=find(cumsum(latent)./sum(latent)>energy,1); coeff=coeff(:,1:idx); %取出的主成分系数矩阵 scores=scores(:,1:idx); %训练样本投影矩阵 %figure; %latent %latent(1:idx,:) %subplot 211;hist(latent);title('样本中的主特征值'); %subplot 212;hist(latent(1:idx,:));title('主成分中的主特征值'); disp('Training has Done'); %% % UIWAIT makes face_recognition wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = face_recognition_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; %%显示提示信息 global training_count str = sprintf('当前每个人的训练样本数为:%d,测试样本数为:%d,请从后%d张人脸中选择测试',training_count,11-training_count,11-training_count); set(handles.text2,'string',str); %% % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %% %从测试样本中选取人脸 global img_target [filename, pathname] = uigetfile({'*.jpg';'*.bmp'},'选择人脸'); str = [pathname, filename]; img_target = imread(str); axes( handles.axes1); img_target=im2double(img_target); %若是彩色图像,则灰度化 if ndims(img_target)==3 img_target=rgb2gray(img_target); end imshow(img_target,[]); %缩放为10*10 img_target=imresize(img_target,[10 10]); % 图像缩放至至10*10 %% % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %% % 测试,进行人脸匹配 global img_target global coeff global scores global training_count global samples_mean %测试样本减去训练样本均值,然后投影,得到投影后的样本表示 score=(img_target(:)'-samples_mean)*coeff; % [1 100]*[100 100] %计算测试样本和每个训练样本之间的欧式距离(计算平方值) %然后利用最近邻分类器对测试样本进行分类 score_repeat = repmat(score,size(scores,1),1);%把1*idx行的一张图片扩展成people_count*training_count*idx,每一行都一样 distance = (scores-score_repeat).^2; disp(distance); [~,idx]=min( sum(distance,2)) ;%对每一行求和,求得最小的一行,就是相当于找最近似的图片 %根据idx找出最近似的训练样本 i = ceil(idx/training_count);%向后取整 j = idx - (i-1)*training_count; img_dst=im2double(imread(['样本库' sprintf('%03d',i) '' sprintf('%02d',j) '.jpg'])); %将寻找到的结果显示出来 if ndims(img_dst)==3 img_dst=rgb2gray(img_dst); end axes( handles.axes2); imshow(img_dst,[]); %%
结果截图:
具体源码和训练库在:
https://download.csdn.net/download/qq_44250808/18624399
最后
以上就是舒服豌豆最近收集整理的关于matlab实现基于主成分分析的人脸识别的全部内容,更多相关matlab实现基于主成分分析内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复