function Ps = gabor(I,w,nu,mu,Kmax,f,sig)
%I表示传入的要进行滤波的图像数据40*30
m = w(1);
n = w(2);
K = Kmax/f^nu * exp(i*mu*pi/8);
Kreal = real(K);
Kimag = imag(K);
NK = Kreal^2+Kimag^2;
Psi = zeros(m,n);
for x = 1:m
for y = 1:n
Z = [x-m/2;y-n/2];
Psi(x,y) = (sig^(-2))*exp((-.5)*NK*(Z(1)^2+Z(2)^2)/(sig^2))*...
(exp(i*[Kreal Kimag]*Z)-exp(-(sig^2)/2));
end
end
%图像数据与gabor的实部和xubu分别进行卷积
Imgabout = conv2(I,double(imag(Psi)),'same');
Regabout = conv2(I,double(real(Psi)),'same');
附录 B 对每幅人脸图像进行处理的源代码
function UltraInfo = handlesinglepic(filename)
if ~ischar(filename)
UltraInfo =0;
end
imma =imread(filename);
[x y] =size(imma); %获取图像的行列数
immqy =zeros(40,30);
G = cell(5,8);
imd =zeros(10,96); %保存gabor的取样点
for s = 1:5
for j = 1:8
G{s,j}=zeros(40,30);
end
end
%%%%%%%这是奇异值分解%%%%%%%%%%%%%%%%%%%%%%%
[U S V] =svd(double(imma));
for ii =1:10
A(ii,1) =S(ii,ii);
end
%%%%%%%%%以下为提取的Gabor幅值特征%%%%%%%%%%%%%
for i =1 :40
for j =1 :30
xz = fix(i*x/41);
yz = fix(j*y/31);
immqy(i,j) =imma(xz,yz);
end
end
for s = 1:5
for j = 1:8
G{s,j}= gabor(immqy,[40 30],(s-1),(j-1),4*pi/5,sqrt(2),3*pi/2);
end
end
%%%%%%%%%%%%%融合前统一量纲%%%%%%%%%%%%%%%%%%%
numave =mean(imd);
numaves =mean(A);
stdnum1 =std(imd);
stdnum2 =std(A);
for ii =1:size(imd,1)
for jj =1:size(imd,2)
imd(ii,jj) =(imd(ii,jj) -numave(jj))/stdnum1(jj);
end
end
for ii =1:size(A,1)
A(ii,1) =(A(ii,1) -numaves)/stdnum2;
End
%%%%%%%%以下串行融合Gabor特征向量与该图像的奇异值%%%%%%%%
UltraInfo =[imd A];
%%%%%%%%%%%%清除多余变量%%%%%%%%%%%%%%%%%%%
clearvarible;
附录 C 最近邻分类的源代码
function mdis =mdist(a,b)
mdis =0;
for ii =1:size(a,1)
for jj =1:size(a,2)
mdis =mdis +(a(ii,jj)-b(ii,jj))^2;
end
end
mdis =sqrt(mdis);
附录 D 程序执行主线源代码
clear;
close all;
clc;
%%%%%%%%%%%读取训练图像和测试图%%%%%%%%%%%%%%%%%
imagedata =cell(1,15); %存储15幅训练图像的特征'开始基准图像文件的读取...'
for i =1:15
imagedata{1,i} =zeros(10,97);
temp =int2str(i);
filename =[temp '.pgm'];
imagedata{1,i} =handlesinglepic(filename);
clc;
['第' int2str(i) '个文件处理完成... 共15幅基准图像']
end
imcompare =cell(1,75);
clc;
'开始比较文件的读取...'
filename =cell(1,75); %存储75幅测试图像
for i =1:75
imcompare{1,i} =zeros(10,97);
temp =int2str(100+i);
filename{i} =[temp '.pgm'];
imcompare{1,i} =handlesinglepic(filename{i});
clc;
['第' int2str(i) '个文件处理完成... 共75幅比较图像']
end
%%%%%比较两种图像的欧式几何距离(最近邻分类),产生结果%%%%%%%%
'正在分析分类图像...'
standardnum = 1e100;
teamnum =1;
fresult =zeros(1,75);
for ii =1:75
standardnum = 1e100;
for jj =1:15
dis =mdist(imagedata{jj},imcompare{ii});
if dis <standardnum
standardnum =dis;
fresult(ii) =jj;
end
end
end
clc;
'比较结束,共1020次比较'
%%%%%%%%%%%图形输出比较后的图像,便于比较识别率%%%%%%%
clc;
'输出原始基准图像...'
figure(1);
for ii =1:15
subplot(3,5,ii);
imshow(imread([int2str(ii) '.pgm']));
title([int2str(ii) '号人物']);
end
clc;
'输出比较后的图像...'
figure(2);
text(50,50,'第1~5人的识别结果(每行为一人)');
printnum =zeros(1,15);
for jj =1:75
if fresult(jj) >=1 & fresult(jj) <=5
printnum(fresult(jj)) =printnum(fresult(jj)) +1;
subplot(5,5,5*(fresult(jj)-1)+printnum(fresult(jj)));
imshow(imread(filename{jj}));
end
end
for i=3:5:23
subplot(5,5,i);
title(['以下为第 ' int2str((i-3)/5+1) ' 个人物的人脸识别结果']);
end
figure(3);
printnum =zeros(1,15);
for jj =1:75
if fresult(jj) >=6 & fresult(jj) <=10
printnum(fresult(jj)) =printnum(fresult(jj)) +1;
subplot(5,5,5*(fresult(jj)-6)+printnum(fresult(jj)));
imshow(imread(filename{jj}));
end
end
for i=3:5:23
subplot(5,5,i);
title(['以下为第 ' int2str((i-3)/5+6) ' 个人物的人脸识别结果']);
end
figure(4);
printnum =zeros(1,15);
for jj =1:75
if fresult(jj) >=11 & fresult(jj) <=15
printnum(fresult(jj)) =printnum(fresult(jj)) +1;
subplot(5,5,5*(fresult(jj)-11)+printnum(fresult(jj)));
imshow(imread(filename{jj}));
end
end
for i=3:5:23
subplot(5,5,i);
title(['以下为第 ' int2str((i-3)/5+11) ' 个人物的人脸识别结果']);
end