人脸识别GUI设计
这是我在理解PCA算法后,设计MATLAB GUI
实现人脸识别。
使用方法:
- 运行face.m主脚本
- 点击
训练机器
选择train文件夹 - 点击
choose photo
选择test文件夹下的一张图片 - 最后点击
recognize
即可进行识别 - 点击
Accuracy
可计算整个test文件夹下所有图识别准确率
效果如下:
在这里插入图片描述](https://img-blog.csdnimg.cn/2019010116254575.png?x-oss-
在这里插入图片描述`附录:MATLAB 核心代码
% 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 im;
[filename, pathname] = uigetfile({’*.bmp’},‘choose photo’);
str = [pathname, filename];
im = imread(str);
axes( handles.axes1);
imshow(im);
% 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 im
global reference
global W
global imgmean
global col_of_data
global pathname
global img_path_list
% 预处理新数据
im = double(im(😃);
objectone = W’*(im - imgmean);
distance = 100000000;
% 最小距离法,寻找和待识别图片最为接近的训练图片
for k = 1:col_of_data
temp = norm(objectone - reference(:,k));
if(distance>temp)
aimone = k;
distance = temp;
aimpath = strcat(pathname, ‘/’, img_path_list(aimone).name);
axes( handles.axes2 )
imshow(aimpath)
end
end
% 显示测试结果
% aimpath = strcat(pathname, ‘/’, img_path_list(aimone).name);
% axes( handles.axes2 )
% imshow(aimpath)
%pushbutton3(训练机器)按钮按下时执行
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global reference
global W
global imgmean
global col_of_data
global pathname
global img_path_list
% 批量读取指定文件夹下的图片128*128
pathname = uigetdir;
img_path_list = dir(strcat(pathname,’*.bmp’));
img_num = length(img_path_list);
imagedata = [];
if img_num >0
for j = 1:img_num
img_name = img_path_list(j).name;
temp = imread(strcat(pathname, ‘/’, img_name));
temp = double(temp(😃);
imagedata = [imagedata, temp];
end
end
col_of_data = size(imagedata,2);
% 中心化 & 计算协方差矩阵
imgmean = mean(imagedata,2);
for i = 1:col_of_data
imagedata(:,i) = imagedata(:,i) - imgmean;
end
covMat = imagedata’*imagedata;
[COEFF, latent, explained] = pcacov(covMat);
% 选择构成95%能量的特征值
i = 1;
proportion = 0;
while(proportion < 95)
proportion = proportion + explained(i);
i = i+1;
end
p = i - 1;
% 特征脸
W = imagedataCOEFF; % NM阶
W = W(:,1:p); % N*p阶
% 训练样本在新座标基下的表达矩阵 p*M
reference = W’*imagedata;