当前位置:   article > 正文

matlab 特征值不排序,matlap 代码求解释!从这里开始即可%对特征值进行排序并去掉...

matlab求特征值 不排序

%人脸识别代码

clear all %    //removes all variables from the workspace. This frees up system memory.

close all %    //Delete specified figure

clc

% number of images on your training set.

%训练集数目

M=16;

%Chosen std and mean.

%It can be any number that it is close to the std and mean of most of the images.

um=100;

ustd=80;

%read and show images(bmp);

%读入M个训练图像并显示在一个窗口上

S=[];   %img matrix

temp=[];

figure(1); %  //figure creates figure graphics objects. Figure objects are the individual windows on the screen in which MATLAB displays graphical output. figure creates a new figure object using default property values.

for i=1:M

str=strcat('D:\民族图形图像研究\jpg\',int2str(i),'.jpg');    %concatenates two strings that form the name of the image int is nteger to string conversion

eval('img=imread(str);');       %  //xecute a string containing a MATLAB expression

img=rgb2gray(img); %rgb 转换成为灰度图像

subplot(ceil(sqrt(M)),ceil(sqrt(M)),i) %  //divides the current figure into rectangular panes m求根行m求根列 i表示第几个图像

imshow(img)

if i==3

title('Training set write by 李锋平','fontsize',18)   %  //Add title to current axes 名字 字体尺寸 字体大小

end

drawnow;  %  //Complete pending drawing events  flushes the event queue and updates the figure window.完成待绘图事件刷新事件队列和更新的数字窗口

[irow icol]=size(img);   % get the number of rows (N1) and columns (N2)获得图像的大小,所有图像的大小要一致

m=irow;n=icol;

for i=1:m

for j=1:n

a(i,j)=img(i,j);

end

end

temp=reshape(a,irow*icol,1);     %creates a (N1*N2)x1 matrix一幅图像构造一个向量 向量的大小和图像大小有关

S=[S temp];         %X is a N1*N2xM matrix after finishing the sequence  生成一个向量矩阵,M个图像有M列

%this is our S

end

%Here we change the mean and std of all images. We normalize all images.

%This is done to reduce the error due to lighting conditions.

%下面是对图像规范化,更具所有图像的的平均值和方差

for i=1:size(S,2)

temp=double(S(:,i));

m=mean(temp);

st=std(temp);

S(:,i)=(temp-m)*ustd/st+um; %%%%%

end

%show normalized images 显示规范化后的图像

figure(2);

for i=1:M

str=strcat(int2str(i),'.bmp');  %重新调整矩阵的行数、列数、维数

img=reshape(S(:,i),irow,icol);

eval('imwrite(img,str)');   % 将图像数据写入到图像文件中,

subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)               %subplot是将多个图画到一个平面上的工具。其中,m表示是图排成m行,n表示图排成n列,也就是整个figure中有n个图是排成一行的,一共m行,如果第一个数字是2就是表示2行图。p是指你现在要把曲线画到figure中哪个图上,最后一个如果是1表示是从左到右第一个位置。

imshow(img)

drawnow;

if i==3

title('Normalized Training Set write by 李锋平','fontsize',18)

end

end

%mean image;显示平均图像,所有图像叠加在一起

m=mean(S,2);   %obtains the mean of each row instead of each column

tmimg=uint8(m);   %converts to unsigned 8-bit integer. Values range from 0 to 255

img=reshape(tmimg,irow,icol);    %takes the N1*N2x1 vector and creates a N2xN1 matrix

figure(3);

imshow(img);

title('Mean Image write by 李锋平','fontsize',18)

% Change image for manipulation

%对图像变换便于处理

dbx=[];   % A matrix

for i=1:M

temp=double(S(:,i));

dbx=[dbx temp];

end

%Covariance matrix C=A'A, L=AA'

%求协方差矩阵

%对于PCA做人脸识别:中心化(也就是你说的去均值)后的矩阵再乘以它的转置矩阵,结果就是协方差矩阵,之后求它的特征值和特征向量了就可以了...

A=dbx';

L=A*A';

% vv are the eigenvector for L

% dd are the eigenvalue for both L=dbx'*dbx and C=dbx*dbx';

[vv dd]=eig(L);

% Sort and eliminate those whose eigenvalue is zero

%对特征值进行排序并去掉0    实数是按照降序排列的,复数是按照虚部的降序排列的

v=[];

d=[];

for i=1:size(vv,2)

if(dd(i,i)>1e-4)

v=[v vv(:,i)];

d=[d dd(i,i)];

end

end

%sort,  will return an ascending sequence

%排序并返回降序的

[B index]=sort(d);

ind=zeros(size(index));

dtemp=zeros(size(index));

vtemp=zeros(size(v));

len=length(index);

for i=1:len

dtemp(i)=B(len+1-i);

ind(i)=len+1-index(i);

vtemp(:,ind(i))=v(:,i);

end

d=dtemp;

v=vtemp;

%Normalization of eigenvectors

%对特征向量进行规范化

for i=1:size(v,2)       %access each column

kk=v(:,i);

temp=sqrt(sum(kk.^2));

v(:,i)=v(:,i)./temp;

end

%Eigenvectors of C matrix

%得到C的特征向量矩阵

u=[];

for i=1:size(v,2)

temp=sqrt(d(i));

u=[u (dbx*v(:,i))./temp];

end

%Normalization of eigenvectors

for i=1:size(u,2)

kk=u(:,i);

temp=sqrt(sum(kk.^2));

u(:,i)=u(:,i)./temp;

end

% show eigenfaces;

%显示特征脸

figure(4);

for i=1:size(u,2)

cmax=max(u(:,i));

cmin=min(u(:,i));

img=(reshape(u(:,i),irow,icol)-cmin)*255/(cmax-cmin);

%img=histeq(img,255);

subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)

imshow(uint8(img))

drawnow;

if i==3

title('Eigenfaces write by 李锋平','fontsize',18)

end

end

% Find the weight of each face in the training set.

%找出训练集中每张脸的权重

omega = [];

for h=1:size(dbx,2)

WW=[];

for i=1:size(u,2)

t = u(:,i)';

WeightOfImage = dot(t,dbx(:,h)');

WW = [WW; WeightOfImage];

end

omega = [omega WW];

end

% Acquire new image

% Note: the input image must have a bmp or jpg extension.

%       It should have the same size as the ones in your training set.

%       It should be placed on your desktop

%获取一张新的脸

%注意:图像的大小和训练集中图像大小一样

%

InputImage = input('Please enter the name of the image and its extension \n','s');

InputImage = imread(strcat('D:\民族图形图像研究\jpg\',InputImage));

InputImage=rgb2gray(InputImage);

figure(5)

subplot(1,2,1)

imshow(InputImage); colormap('gray');title('Input image','fontsize',18)

InImage=reshape(double(InputImage)',irow*icol,1);

temp=InImage;

me=mean(temp);

st=std(temp);

temp=(temp-me)*ustd/st+um;

NormImage = temp;

Difference = temp-m;

NormImage = Difference;

p = [];

aa=size(u,2);

for i = 1:aa

pare = dot(NormImage,u(:,i));

p = [p; pare];

end

ReshapedImage = m + u(:,1:aa)*p;    %m is the mean image, u is the eigenvector

ReshapedImage = reshape(ReshapedImage,irow,icol);

%ReshapedImage = ReshapedImage';

%show the reconstructed image. 显示重构的图像

subplot(1,2,2)

imshow(uint8(ReshapedImage)); colormap('gray');

title('Reconstructed image write by 李锋平','fontsize',18)

InImWeight = [];

for i=1:size(u,2)

t = u(:,i)';

WeightOfInputImage = dot(t,Difference');

InImWeight = [InImWeight; WeightOfInputImage];

end

ll = 1:M;

figure(68)

subplot(1,2,1)

stem(ll,InImWeight)

title('Weight of Input Face write by 李锋平','fontsize',14)

% Find Euclidean distance 查找Euclidean距离

e=[];

for i=1:size(omega,2)

q = omega(:,i);

DiffWeight = InImWeight-q;

mag = norm(DiffWeight);

e = [e mag];

end

kk = 1:size(e,2);

subplot(1,2,2)

stem(kk,e)

title('Eucledian distance of input image,write by 李锋平','fontsize',14)

MaximumValue=max(e)

MinimumValue=min(e)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/70091
推荐阅读
相关标签
  

闽ICP备14008679号