clc;
clear all;
Class_train_num = 12; %%%%每个类选择的训练样本个数
Class_num = 68; %%%%%类别个数
Class_image_num = 24; %%%%%每个类的图象总数
Class_test_num = Class_image_num-Class_train_num; %%%%%每个类选择的测试样本个数
Train_num = Class_num*Class_train_num;
Test_num = Class_num*Class_test_num;
t=3e+8;
%=======================================================================
fid=fopen('E:\jieguo\2DDLPP\PIE.txt','a');
fprintf(fid,'%20s','设置参数:');
fprintf(fid,'%12s','t:');
fprintf(fid,'%12d\n',t);
fprintf(fid,'%20s','Class number:');
fprintf(fid,'%12g',Class_num);
fprintf(fid,'%20s','Image number:');
fprintf(fid,'%12g',Class_image_num);
fprintf(fid,'%20s','Training number:');
fprintf(fid,'%12g\n',Class_train_num);
%===================================================================
load('D:\database\Pose29_64x64.mat');
for i = 1 : Class_num
for j = 1 : Class_train_num
index = (i-1)*Class_train_num+j;
image_index=(i-1)*Class_image_num+j;
a = double(reshape(fea(image_index,:),[64 64]));
train_matrix(:,:,index)=a;
clear a;
end
end
%%%%%%%%%%%%%%%%%%%% reading test images %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear index;
clear image_index;
clear i;
clear j;
for i = 1 : Class_num
for j = 1 : Class_test_num
index = (i-1)*Class_test_num+j;
image_index=(i-1)*Class_image_num+Class_train_num+j;
b= double(reshape(fea(image_index,:),[64 64]));
test_matrix(:,:,index) = b; %%%%%%读入训练图像,维数为row*(Class_num*Class_train_num*column)
clear b;
end
end
clear index;
clear image_index;
clear i;
clear j;
%%%%%%%每一类的均值
for i = 1 : Class_num
Class_mean(:,:,i) = mean(train_matrix(:,:,(i-1)*Class_train_num+1:i*Class_train_num),3);
end
for i = 1:Class_num
f(Class_train_num *(i-1)+1:Class_train_num *i,1)= ones(Class_train_num ,1)*i;%%%类别
end
options.gnd = f;
%%%%%%%%训练样本间的距离
Euc_Distance=zeros(Train_num,Train_num);
for i=1:Train_num-1
for j=i+1:Train_num
Euc_Distance(i,j)=norm(train_matrix(:,:,i)-train_matrix(:,:,j),'fro'); %the distance between Xi and Xj
end
end
Euc_Distance=Euc_Distance+Euc_Distance';
clear i j;
%%%%%%%%类别间的距离
Class_Euc_Distance=zeros(Class_num,Class_num);
for i=1:Class_num-1
for j=i+1:Class_num
Class_Euc_Distance(i,j)=norm(Class_mean(:,:,i)-Class_mean(:,:,j),'fro'); %the distance between Xi and Xj
end
end
Class_Euc_Distance=Class_Euc_Distance+Class_Euc_Distance';
clear i j;
%%%%%%%判断类别
F = zeros(Train_num,Train_num);
Label = unique(options.gnd);
nLabel = length(Label);
for idx=1:nLabel
classIdx = find(options.gnd==Label(idx));
F(classIdx,classIdx) = 1;%%%同类
end
%%%%%%%计算2个权重矩阵
S=zeros(Train_num,Train_num);
B=zeros(Class_num,Class_num);
DDD=exp(-(Euc_Distance.^2)./t);
B=exp(-(Class_Euc_Distance.^2)./t);
S=F.*DDD; %%%%%%%%%
[a,b]=size(train_matrix(:,:,1)');
G=zeros(a,a);
GG=zeros(a,a);
for i = 1 : Class_num
for j = 1 : Class_train_num
for k = 1 : Class_train_num
L1=train_matrix(:,:,(i-1)*Class_train_num+j)-train_matrix(:,:,(i-1)*Class_train_num+k);
G=G+L1'*S(i,j)*L1;
end
end
end
for i = 1 : Class_num
for j = 1 : Class_num
L2=Class_mean(:,:,i)-Class_mean(:,:,j);
GG=GG+L2'*B(i,j)*L2;
end
end
[v,d]=eig(G,GG);
D2=diag(d);
[eigvalues,Index]=sort(D2);
eigvectors=v(:,Index);
for i=1:Train_num
Y(:,:,i)=train_matrix(:,:,i)*eigvectors; %%训练样本的LPP投影矩阵
end
for j=1:Test_num
Z(:,:,j)=test_matrix(:,:,j)*eigvectors;
end
clear i j d;
FNUM=20;
for PNUM = FNUM : -1 : 1
%%%%%%%%%%%%%%%%%% calculating similarity %%%
for i=1:Test_num
for j=1:Train_num
DF = Z(:,1:PNUM,i)-Y(:,1:PNUM,j);
% d(j,i)=norm(Z(:,1:PNUM,i)-Y(:,1:PNUM,j),2);
d(j,i) = sum(sqrt(diag(DF'*DF)));
end
end
[K,L] = min(d);
L = fix((L-1)/Class_train_num+1);
fresult =[];
for i =1 :Class_num
fresult=[fresult i*ones(1,Class_test_num)];
end
error = 0;
for i = 1:Test_num
if L(i) ~= fresult(i)
error = error+1;
end
end
fprintf(fid,'%20s','Feature number:');
fprintf(fid,'%12g',PNUM); fprintf(fid,'%12s','L2');
fprintf(fid,'%12g',(1-error/(Test_num))*100);
fprintf(fid,'\r\n%12g','');
end
fclose(fid);
阿里matlab建模师
- 粉丝: 4237
- 资源: 2840
最新资源
- 微电网(两台)主从控制孤岛-并网平滑切的分析 分析了: 1.孤岛下VF控制 2.并网下PQ控制 3.孤岛下主从控制 4.孤岛到并网的平滑切控制 5.除模型外还对分布式发电与主动配电网一些常见问题做了
- 第四组二手产品.zip
- MVIMG_20241222_194113.jpg
- 基于小程序的在线疫苗预约小程序源代码(java+小程序+mysql+LW).zip
- 基于小程序的岳阳市美术馆预约平台源代码(java+小程序+mysql+LW).zip
- 基于小程序的音乐播放器小程序源代码(java+小程序+mysql+LW).zip
- 多功能知识付费源码下载实现流量互导多渠道变现+搭建教程
- 3. Kafka入门-安装与基本命令
- 基于小程序的养老院管理系统源代码(java+小程序+mysql+LW).zip
- 基于小程序的新冠抗原自测平台小程序源代码(java+小程序+mysql+LW).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈