clear all;
close all;
clc;
for i = 1:40
for j = 1:10
path = strcat('C:\Users\超级霸怪\Desktop\pca+svm\face\s', num2str(i), '\', num2str(j), '.pgm');
filedata{i,j} = imread(path);
%imshow(filedata{i,j});
end
end
[m,n] = size(filedata{1,1});
num =6;%选5张脸当成样本,
for i =1:40
%n_rand = randperm(10);%随机选
for j = 1:num
train_pic{(i-1)*num+j}=filedata{i,j};
end
for k = 1:(10-num)
test_pic{(i-1)*(10-num)+k}=filedata{i,(k+num)};
end
end
for i = 1:40*num
train_face(i,:)=reshape(train_pic{i},1,m*n);
end
for i=1:40*(10-num)
test_face(i,:) = reshape(test_pic{i},1,m*n);
end
accuracy = zeros(1,30);
K = 30;
;%前K个特征值
img = double(train_face);
[m1,n1] = size(img);
img_mean = mean(img);%每列平均值
img_mean_all = repmat(img_mean,m1,1);
Z = img -img_mean_all;
T = Z*Z';%协方差矩阵
[V,D]=eigs(T,K);%计算T中最大的前k个特征值与特征向量,D特征值,V特征向量,已从大到小排列
a =V;
%b =D;
V=Z'*V;
%c = V;
for i = 1:K%特征向量单位化
l = norm(V(:,i));
V(:,i) = V(:,i)/l;
end
img_new = Z*V;%低维度下的各个脸的数据
num_train = size(img_new,1);%训练脸总数
num_test = size(test_face,1);%测试脸总数
true_num = 0;
for i = 1:num_test
pic = double(test_face(i,:));
pic1 = pic - img_mean;
pic2 = pic1*V;
for j = 1:num_train
error(j)= norm(img_new(j,:)-pic2);%求取范数距离
end
face_find = find(error<=min(error));%取最小者
%e = face_find;
face_find = ceil(face_find/num);%向上取整 表示识别的是第几个人的脸
true_face = ceil(i/(10-num));%真实的是第几个人的脸
if face_find == true_face
true_num = true_num+1;%正确分类的个数
end
end
accuracy = true_num/num_test;
% end
% figure
% t = 1:30
% plot(t,accuracy,'-bo');
% legend('范数');
% xlabel('特征值个数');
% ylabel('识别率');
林当时
- 粉丝: 114
- 资源: 1万+
最新资源
- 3b116应急物资供应管理系统_springboot+vue.zip
- 3b119灾情救援系统_springboot+vue0.zip
- 3b117应急知识学习系统_springboot+vue.zip
- 产品展示交易平台源代码.zip
- Win64OpenSSL 工具
- 3b118员工日志管理信息系统_springboot+vue.zip
- 车票网上预订系统源代码.zip
- 宠物系统源代码.zip
- 大创管理系统源代码.zip
- 3b120在线仓库_springboot+vue0.zip
- 3b121在线考试系统_springboot+vue.zip
- 大学生就业招聘系统源代码.zip
- 电影评论网站系统源代码.zip
- 房产销售系统源代码.zip
- 蓝桥杯赛场资源包里的LCD驱动和芯片资源包V1.2、V1.3、V1.4 其余资料自行下载,全部资料太大了上传不了
- 3b122智能排课系统_springboot+vue.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈